All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.networknt.jsonoverlay.Overlay Maven / Gradle / Ivy

Go to download

A fork of RepreZen JsonOverlay with all dependencies and code generation removed

There is a newer version: 2.1.36
Show newest version
/*********************************************************************
 *  Copyright (c) 2017 ModelSolv, Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *     ModelSolv, Inc.
 *     - initial API and implementation and/or initial documentation
 **********************************************************************/
package com.networknt.jsonoverlay;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.jsonoverlay.PositionInfo.PositionEndpoint;

public class Overlay {

	private JsonOverlay overlay;

	public Overlay(JsonOverlay overlay) {
		this.overlay = overlay;
	}

	public static  Overlay of(JsonOverlay overlay) {
		return new Overlay(overlay);
	}

	public Overlay(IJsonOverlay overlay) {
		@SuppressWarnings("unchecked")
		JsonOverlay castOverlay = (JsonOverlay) overlay;
		this.overlay = castOverlay;
	}

	public static  Overlay of(IJsonOverlay overlay) {
		return new Overlay(overlay);
	}

	public static  Overlay> of(MapOverlay overlay) {
		return new Overlay>(overlay);
	}

	public static  Overlay> of(Map map) {
		MapOverlay overlay = getSidebandOverlay(map);
		return overlay != null ? new Overlay>(overlay) : null;
	}

	public static  Overlay> of(ListOverlay overlay) {
		return new Overlay>(overlay);
	}

	public static  Overlay> of(List list) {
		ListOverlay overlay = getSidebandOverlay(list);
		return overlay != null ? new Overlay>(overlay) : null;
	}

	public Overlay(MapOverlay map, String key) {
		this.overlay = map._getOverlay(key);
	}

	public static  Overlay of(MapOverlay map, String key) {
		return new Overlay(map, key);
	}

	public static  Overlay of(Map map, String key) {
		Overlay> mapOverlay = Overlay.of(map);
		return mapOverlay != null ? Overlay.of((MapOverlay) mapOverlay.getOverlay(), key) : null;
	}

	public Overlay(ListOverlay list, int index) {
		this.overlay = list._getOverlay(index);
	}

	public static  Overlay of(ListOverlay list, int index) {
		return new Overlay(list, index);
	}

	public static  Overlay of(List list, int index) {
		Overlay> listOverlay = Overlay.of(list);
		return listOverlay != null ? Overlay.of((ListOverlay) listOverlay.getOverlay(), index) : null;
	}

	public Overlay(PropertiesOverlay props, String fieldName) {
		@SuppressWarnings("unchecked")
		JsonOverlay overlay = (JsonOverlay) props._getOverlay(fieldName);
		this.overlay = overlay;
	}

	public static  Overlay of(PropertiesOverlay props, String fieldName, Class type) {
		return new Overlay(props, fieldName);
	}

	public static  Overlay of(IJsonOverlay props, String fieldName, Class type) {
		if (props instanceof PropertiesOverlay) {
			@SuppressWarnings("unchecked")
			PropertiesOverlay castProps = (PropertiesOverlay) props;
			return Overlay.of(castProps, fieldName, type);
		} else {
			return null;
		}
	}

	public final V get() {
		return overlay._get();
	}

	public static  V get(JsonOverlay overlay) {
		return overlay._get();
	}

	public final JsonOverlay getOverlay() {
		return overlay;
	}

	public static  ListOverlay getListOverlay(Overlay> overlay) {
		if (overlay.getOverlay() instanceof ListOverlay) {
			ListOverlay castOverlay = getSidebandOverlay(overlay.get());
			return castOverlay;
		} else {
			return null;
		}
	}

	public static  MapOverlay getMapOverlay(Overlay> overlay) {
		if (overlay.getOverlay() instanceof MapOverlay) {
			MapOverlay castOverlay = getSidebandOverlay(overlay.get());
			return castOverlay;
		} else {
			return null;
		}
	}

	public static PropertiesOverlay getPropertiesOverlay(Overlay> overlay) {
		if (overlay.getOverlay() instanceof PropertiesOverlay) {
			return (PropertiesOverlay) overlay.getOverlay();
		} else {
			return null;
		}
	}

	public JsonOverlay find(JsonPointer path) {
		return overlay._find(path);
	}

	public static JsonOverlay find(JsonOverlay overlay, JsonPointer path) {
		return overlay._find(path);
	}

	public JsonOverlay find(String path) {
		return overlay._find(path);
	}

	public static > JsonOverlay find(OV overlay, String path) {
		return overlay._find(path);
	}

	public JsonNode toJson() {
		return overlay._toJson();
	}

	public static  JsonNode toJson(JsonOverlay overlay) {
		return overlay._toJson();
	}

	public JsonNode toJson(SerializationOptions options) {
		return overlay._toJson(options);
	}

	public static  JsonNode toJson(JsonOverlay overlay, SerializationOptions options) {
		return overlay._toJson(options);
	}

	public JsonNode toJson(SerializationOptions.Option... options) {
		return overlay._toJson(options);
	}

	public static  JsonNode toJson(JsonOverlay overlay, SerializationOptions.Option... options) {
		return overlay._toJson(options);
	}

	public JsonNode getParsedJson() {
		return overlay._getParsedJson();
	}

	public static JsonNode getParsedJson(JsonOverlay overlay) {
		return overlay._getParsedJson();
	}

	public boolean isPresent() {
		return overlay._isPresent();
	}

	public static  boolean isPresent(JsonOverlay overlay) {
		return overlay._isPresent();
	}

	public boolean isElaborated() {
		return overlay._isElaborated();
	}

	public static  boolean isElaborated(JsonOverlay overlay) {
		return overlay._isElaborated();
	}

	public JsonOverlay getParent() {
		return overlay._getParent();
	}

	public static  JsonOverlay getParent(JsonOverlay overlay) {
		return overlay._getParent();
	}

	public PropertiesOverlay getParentPropertiesOverlay() {
		JsonOverlay parent = getParent();
		while (parent != null) {
			if (parent instanceof PropertiesOverlay) {
				return (PropertiesOverlay) parent;
			} else {
				parent = getParent(parent);
			}
		}
		return null;
	}

	public static  PropertiesOverlay getParentPropertiesOverlay(JsonOverlay overlay) {
		return new Overlay(overlay).getParentPropertiesOverlay();
	}

	public String getPathInParent() {
		return overlay._getPathInParent();
	}

	public static String getPathInParent(JsonOverlay overlay) {
		return overlay._getPathInParent();
	}

	public JsonOverlay getRoot() {
		return overlay._getRoot();
	}

	public static JsonOverlay getRoot(JsonOverlay overlay) {
		return overlay._getRoot();
	}

	public  Model getModel() {
		@SuppressWarnings("unchecked")
		Model model = (Model) overlay._getModel();
		return model;
	}

	public static  Model getModel(JsonOverlay overlay) {
		return new Overlay(overlay).getModel();
	}

	public String getPathFromRoot() {
		return overlay._getPathFromRoot();
	}

	public static String getPathFromFromRoot(JsonOverlay overlay) {
		return overlay._getPathFromRoot();
	}

	public String getJsonReference() {
		return overlay._getJsonReference();
	}

	public static String getJsonReference(JsonOverlay overlay) {
		return overlay._getJsonReference();
	}

	public String getJsonReference(boolean forRef) {
		return overlay._getJsonReference(forRef);
	}

	public static String getJsonReference(JsonOverlay overlay, boolean forRef) {
		return overlay._getJsonReference(forRef);
	}

	public Optional getPositionInfo() {
		return overlay._getPositionInfo();
	}

	public static Optional getPositionInfo(JsonOverlay overlay) {
		return overlay._getPositionInfo();
	}

	public Optional getStartPosition() {
		return overlay._getPositionInfo().map(info -> info.getStart());
	}

	public static Optional getStartPosition(JsonOverlay overlay) {
		return overlay._getPositionInfo().map(info -> info.getStart());
	}

	public List getPropertyNames() {
		if (overlay instanceof PropertiesOverlay) {
			return ((PropertiesOverlay) overlay)._getPropertyNames();
		} else {
			return null;
		}
	}

	public static  List getPropertyNames(JsonOverlay overlay) {
		return new Overlay(overlay).getPropertyNames();
	}

	public boolean isReference(String key) {
		return getReference(key) != null;
	}

	public static  boolean isReference(JsonOverlay overlay, String key) {
		return new Overlay(overlay).isReference(key);
	}

	public boolean isReference(int index) {
		return getReference(index) != null;
	}

	public static  boolean isReference(JsonOverlay overlay, int index) {
		return new Overlay(overlay).getReference(index) != null;
	}

	public Overlay getReferenceOverlay() {
		RefOverlay refOverlay = overlay._getRefOverlay();
		return refOverlay != null ? new Overlay(refOverlay.getOverlay()) : null;
	}

	public static  Overlay getReferenceOverlay(JsonOverlay overlay) {
		return Overlay.of(overlay).getReferenceOverlay();
	}

	public Reference getReference(String key) {
		if (overlay instanceof PropertiesOverlay) {
			return getPropertyReference(key);
		} else if (overlay instanceof MapOverlay) {
			return getMapReference(key);
		} else {
			return null;
		}
	}

	public static  Reference getReference(JsonOverlay overlay, String key) {
		return new Overlay(overlay).getReference(key);
	}

	public Reference getReference(int index) {
		if (overlay instanceof ListOverlay) {
			return getListReference(index);
		} else {
			return null;
		}
	}

	public static  Reference getReference(JsonOverlay overlay, int index) {
		return new Overlay(overlay).getReference(index);
	}

	private Reference getPropertyReference(String name) {
		PropertiesOverlay propsOverlay = (PropertiesOverlay) overlay;
		return getReference(propsOverlay._getOverlay(name));
	}

	private Reference getMapReference(String key) {
		@SuppressWarnings("unchecked")
		MapOverlay mapOverlay = (MapOverlay) overlay;
		return getReference(mapOverlay._getOverlay(key));
	}

	private Reference getListReference(int index) {
		@SuppressWarnings("unchecked")
		ListOverlay listOverlay = (ListOverlay) overlay;
		return getReference(listOverlay._getOverlay(index));
	}

	private Reference getReference(JsonOverlay overlay) {
		return overlay != null ? overlay._getReference() : null;
	}

	private static  ListOverlay getSidebandOverlay(List list) {
		try {
			@SuppressWarnings("unchecked")
			ListOverlay castOverlay = (ListOverlay) list.getClass().getMethod("getOverlay").invoke(list);
			return castOverlay;
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e) {
			return null;
		}
	}

	private static  MapOverlay getSidebandOverlay(Map map) {
		try {
			@SuppressWarnings("unchecked")
			MapOverlay castOverlay = (MapOverlay) map.getClass().getMethod("getOverlay").invoke(map);
			return castOverlay;
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e) {
			return null;
		}
	}

	@Override
	public String toString() {
		return overlay.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy