Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.networknt.oas.jsonoverlay.MapOverlay Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2017 ModelSolv, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* ModelSolv, Inc. - initial API and implementation and/or initial documentation
*******************************************************************************/
package com.networknt.oas.jsonoverlay;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.networknt.oas.jsonoverlay.SerializationOptions.Option;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
public class MapOverlay> extends JsonOverlay> {
private Map> overlays = new LinkedHashMap<>();
private OverlayFactory valueFactory;
private Pattern keyPattern;
public MapOverlay(Map value, JsonOverlay> parent, OverlayFactory valueFactory,
Pattern keyPattern, ReferenceRegistry refReg) {
super(value, parent, refReg);
this.valueFactory = valueFactory;
this.keyPattern = keyPattern;
fillWithValues();
}
public MapOverlay(JsonNode json, JsonOverlay> parent, OverlayFactory valueFactory, Pattern keyPattern,
ReferenceRegistry refReg) {
super(json, parent, refReg);
this.valueFactory = valueFactory;
this.keyPattern = keyPattern;
fillWithJson();
}
private void fillWithValues() {
overlays.clear();
if (value != null) {
for (Entry entry : value.entrySet()) {
overlays.put(entry.getKey(),
new ChildOverlay<>(entry.getKey(), entry.getValue(), this, valueFactory, refReg));
}
}
}
private void fillWithJson() {
value.clear();
overlays.clear();
for (Entry field : iterable(json.fields())) {
String key = field.getKey();
if (keyPattern == null || keyPattern.matcher(key).matches()) {
ChildOverlay overlay = new ChildOverlay(key, json.get(key), this, valueFactory, refReg);
overlay.getOverlay().setPathInParent(key);
overlays.put(key, overlay);
value.put(key, overlay.get(false));
}
}
}
@Override
public Map get(boolean elaborate) {
return value;
}
@Override
public IJsonOverlay> _find(JsonPointer path) {
String key = path.getMatchingProperty();
return overlays.containsKey(key) ? overlays.get(key).find(path.tail()) : null;
}
@Override
protected Map fromJson(JsonNode json) {
return new HashMap<>();
}
@Override
public JsonNode toJson(SerializationOptions options) {
ObjectNode obj = jsonObject();
for (Entry> entry : overlays.entrySet()) {
obj.set(entry.getKey(), entry.getValue().toJson(options.plus(Option.KEEP_ONE_EMPTY)));
}
return obj.size() > 0 || options.isKeepThisEmpty() ? obj : jsonMissing();
}
public boolean containsKey(String name) {
return overlays.containsKey(name);
}
public V get(String name) {
IJsonOverlay overlay = overlays.get(name);
return overlay != null ? overlay.get() : null;
}
protected IJsonOverlay getOverlay(String name) {
return overlays.get(name);
}
public void set(String name, V value) {
overlays.put(name, valueFactory.create(value, this, refReg));
}
public void remove(String name) {
overlays.remove(name);
}
public int size() {
return overlays.size();
}
public Pattern getKeyPattern() {
return keyPattern;
}
public boolean isReference(String key) {
@SuppressWarnings("unchecked")
ChildOverlay childOverlay = (ChildOverlay) overlays.get(key);
return childOverlay.isReference();
}
public Reference getReference(String key) {
@SuppressWarnings("unchecked")
ChildOverlay childOverlay = (ChildOverlay) overlays.get(key);
return childOverlay.getReference();
}
public static > OverlayFactory, MapOverlay> getFactory(
OverlayFactory valueFactory, String keyPattern) {
return new MapOverlayFactory(valueFactory, getWholeMatchPattern(keyPattern));
}
private static Pattern getWholeMatchPattern(String pat) {
return pat != null ? Pattern.compile("^" + pat + "$") : null;
}
protected static class MapOverlayFactory>
extends OverlayFactory, MapOverlay> {
private OverlayFactory valueFactory;
private Pattern keyPattern;
public MapOverlayFactory(OverlayFactory valueFactory, Pattern keyPattern) {
this.valueFactory = valueFactory;
this.keyPattern = keyPattern;
}
public Pattern getKeyPattern() {
return keyPattern;
}
@Override
protected Class super MapOverlay> getOverlayClass() {
return MapOverlay.class;
}
@Override
public MapOverlay _create(Map value, JsonOverlay> parent, ReferenceRegistry refReg) {
return new MapOverlay(value, parent, valueFactory, keyPattern, refReg);
}
@Override
public MapOverlay _create(JsonNode json, JsonOverlay> parent, ReferenceRegistry refReg) {
return new MapOverlay(json, parent, valueFactory, keyPattern, refReg);
}
}
}