View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.inspection.api.util;
23  
24  import com.google.gson.Gson;
25  import com.google.gson.reflect.TypeToken;
26  
27  import java.util.Collections;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  /**
32   * This utility class provides some utility functions to handle the options of the media inspection service.
33   * This class is thread-safe.
34   */
35  public final class Options {
36  
37    /* Empty map of options indicating no options set */
38    public static final Map<String, String> NO_OPTION = Collections.unmodifiableMap(new HashMap<String, String>());
39  
40    /* Used to parse JSON. Gson is thread-safe */
41    private static final Gson gson = new Gson();
42  
43    /* Hide utility class constructor */
44    private Options() { };
45  
46    /**
47     * Parse the media inspection service options JSON string
48     *
49     * @param options
50     *          Options in form of a JSON string
51     * @return
52     *          Options as Java map data structure
53     */
54    public static Map<String, String> fromJson(String options) {
55      Map<String, String> result = null;
56      if (options != null) {
57        result = gson.fromJson(options, new TypeToken<Map<String, String>>() { }.getType());
58      } else {
59        result = NO_OPTION;
60      }
61      return result;
62    }
63  
64    /**
65     * Transform media inspection service options to a JSON string
66     *
67     * @param options
68     *          Media inspection service options
69     * @return
70     *          Media inspection service options represented by a JSON string
71     */
72    public static String toJson(Map<String, String> options) {
73      String result = null;
74      if (options != null) {
75        result = gson.toJson(options);
76      } else {
77        result = gson.toJson(NO_OPTION);
78      }
79      return result;
80    }
81  
82  }