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