View Javadoc
1   /*
2    * Copyright 2018-2022 the original author or authors.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.geojson.model;
18  
19  import com.fasterxml.jackson.annotation.JsonAnyGetter;
20  import com.fasterxml.jackson.annotation.JsonAnySetter;
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import io.swagger.v3.oas.annotations.media.Schema;
23  import java.util.Collections;
24  import java.util.LinkedHashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Optional;
28  import java.util.StringTokenizer;
29  import lombok.AllArgsConstructor;
30  import lombok.EqualsAndHashCode;
31  import lombok.NoArgsConstructor;
32  import lombok.ToString;
33  
34  /**
35   * This base class stores unknown json properties.
36   *
37   * @author Christian Bremer
38   */
39  @ToString
40  @EqualsAndHashCode
41  @NoArgsConstructor
42  @AllArgsConstructor
43  public abstract class UnknownAware {
44  
45    @Schema(description = "Unknown properties.", hidden = true)
46    @JsonIgnore
47    private Map<String, Object> unknown;
48  
49    /**
50     * Gets the unknown json properties (can be {@code null}).
51     *
52     * @return the unknown
53     */
54    @JsonAnyGetter
55    public Map<String, Object> unknown() {
56      return unknown;
57    }
58  
59    /**
60     * Sets the unknown json properties.
61     *
62     * @param unknown the unknown json properties
63     */
64    public void unknown(Map<String, Object> unknown) {
65      if (unknown != null && !unknown.isEmpty()) {
66        this.unknown = unknown;
67      }
68    }
69  
70    /**
71     * Any json setter.
72     *
73     * @param name the name
74     * @param value the value
75     */
76    @JsonAnySetter
77    public void unknown(String name, Object value) {
78      if (name == null || name.trim().isEmpty()) {
79        return;
80      }
81      if (unknown == null) {
82        unknown = new LinkedHashMap<>();
83      }
84      unknown.put(name, value);
85    }
86  
87    /**
88     * Returns {@code true} if there are unknown properties, otherwise {@code false}.
89     *
90     * @return {@code true} if there are unknown properties, otherwise {@code false}
91     */
92    public boolean hasUnknown() {
93      return unknown != null && !unknown.isEmpty();
94    }
95  
96    /**
97     * Find a value from the unknown map.
98     *
99     * @param <T> the class type
100    * @param jsonPath the json path, e. g. {@code $.firstKey.secondKey.thirdKey}
101    * @param clazz the expected result class
102    * @return an empty optional if the value was not found or can not be casted, otherwise the value
103    */
104   @SuppressWarnings({"unchecked", "rawtypes"})
105   public <T> Optional<T> findUnknown(String jsonPath, Class<T> clazz) {
106     if (!hasUnknown() || !isJsonPath(jsonPath) || clazz == null) {
107       return Optional.empty();
108     }
109     Object value = null;
110     Map<String, Object> tmpUnknown = unknown;
111     StringTokenizer tokenizer = new StringTokenizer(jsonPath.substring(2), ".");
112     while (tokenizer.hasMoreTokens()) {
113       String token = tokenizer.nextToken();
114       value = tmpUnknown.get(token);
115       if (value == null) {
116         break;
117       }
118       if (value instanceof Map && tokenizer.hasMoreTokens()) {
119         try {
120           tmpUnknown = (Map) value;
121         } catch (Exception e) {
122           return Optional.empty();
123         }
124       }
125     }
126     if (value == null) {
127       return Optional.empty();
128     }
129     try {
130       return Optional.of(clazz.cast(value));
131     } catch (Exception e) {
132       return Optional.empty();
133     }
134   }
135 
136   /**
137    * Find a list from the unknown map.
138    *
139    * @param <E> the list element type
140    * @param jsonPath the json path, e. g. {@code $.firstKey.secondKey.thirdKey}
141    * @param clazz the list element type
142    * @return an empty optional if the list was not found or can not be casted, otherwise the list
143    */
144   @SuppressWarnings({"Convert2MethodRef", "unchecked"})
145   public <E> Optional<List<E>> findUnknownList(String jsonPath, Class<E> clazz) {
146     if (clazz == null) {
147       return Optional.empty();
148     }
149     try {
150       return findUnknown(jsonPath, List.class).map(
151           list -> Collections.unmodifiableList(list));
152 
153     } catch (RuntimeException ignored) {
154       return Optional.empty();
155     }
156   }
157 
158   /**
159    * Find a map / json object from the unknown map.
160    *
161    * @param jsonPath the json path, e. g. {@code $.firstKey.secondKey.thirdKey}
162    * @return an empty optional if the map / json object was not found or can not be cast,
163    *     otherwise the map / json object
164    */
165   public Optional<Map<String, Object>> findUnknownMap(String jsonPath) {
166     try {
167       //noinspection unchecked,Convert2MethodRef
168       return findUnknown(jsonPath, Map.class)
169           .map(map -> Collections.unmodifiableMap(map));
170 
171     } catch (RuntimeException ignored) {
172       return Optional.empty();
173     }
174   }
175 
176   private boolean isJsonPath(String jsonPath) {
177     return jsonPath != null && jsonPath.startsWith("$.") && jsonPath.length() > 2;
178   }
179 
180 }