View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.util;
17  
18  import java.util.Date;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.codelibs.fess.taglib.FessFunctions;
23  
24  public final class DocumentUtil {
25  
26      private DocumentUtil() {
27      }
28  
29      public static <T> T getValue(final Map<String, Object> doc, final String key, final Class<T> clazz, final T defaultValue) {
30          T value = getValue(doc, key, clazz);
31          if (value == null) {
32              return defaultValue;
33          }
34          return value;
35      }
36  
37      @SuppressWarnings("unchecked")
38      public static <T> T getValue(final Map<String, Object> doc, final String key, final Class<T> clazz) {
39          if (doc == null || key == null) {
40              return null;
41          }
42  
43          final Object value = doc.get(key);
44          if (value == null) {
45              return null;
46          }
47  
48          if (value instanceof List) {
49              if (clazz.isAssignableFrom(List.class)) {
50                  return (T) value;
51              }
52  
53              if (((List<?>) value).isEmpty()) {
54                  return null;
55              }
56  
57              return convertObj(((List<?>) value).get(0), clazz);
58          }
59  
60          return convertObj(value, clazz);
61      }
62  
63      @SuppressWarnings("unchecked")
64      private static <T> T convertObj(final Object value, final Class<T> clazz) {
65          if (value == null) {
66              return null;
67          }
68  
69          if (clazz.isAssignableFrom(String.class)) {
70              return (T) value.toString();
71          } else if (clazz.isAssignableFrom(Date.class)) {
72              if (value instanceof Date) {
73                  return (T) value;
74              } else {
75                  return (T) FessFunctions.parseDate(value.toString());
76              }
77          } else if (clazz.isAssignableFrom(Long.class)) {
78              if (value instanceof Long) {
79                  return (T) value;
80              } else {
81                  return (T) Long.valueOf(value.toString());
82              }
83          } else if (clazz.isAssignableFrom(Integer.class)) {
84              if (value instanceof Integer) {
85                  return (T) value;
86              } else {
87                  return (T) Integer.valueOf(value.toString());
88              }
89          } else if (clazz.isAssignableFrom(Double.class)) {
90              if (value instanceof Double) {
91                  return (T) value;
92              } else {
93                  return (T) Double.valueOf(value.toString());
94              }
95          } else if (clazz.isAssignableFrom(Float.class)) {
96              if (value instanceof Float) {
97                  return (T) value;
98              } else {
99                  return (T) Float.valueOf(value.toString());
100             }
101         } else if (clazz.isAssignableFrom(Boolean.class)) {
102             if (value instanceof Boolean) {
103                 return (T) value;
104             } else {
105                 return (T) Boolean.valueOf(value.toString());
106             }
107         }
108         return null;
109     }
110 }