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

com.networknt.jsonoverlay.OverlayFactory 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 com.fasterxml.jackson.databind.JsonNode;

public abstract class OverlayFactory {

	private final Class> overlayClass = getOverlayClass();

	public JsonOverlay create(V value, JsonOverlay parent, ReferenceManager refMgr) {
		JsonOverlay overlay = _create(value, parent, refMgr);
		overlay._elaborate(true);
		return overlay;
	}

	@SuppressWarnings("unchecked")
	public JsonOverlay create(JsonNode json, JsonOverlay parent, ReferenceManager refMgr) {
		JsonOverlay overlay;
		if (Reference.isReferenceNode(json)) {
			Reference reference = refMgr.getReference(json);
			RefOverlay refOverlay = new RefOverlay(reference, null, this, refMgr);
			overlay = refOverlay.getOverlay();
			if (overlay == null) {
				overlay = _create((V) null, parent, refMgr);
			}
			if (overlay != null) {
				overlay = ((OverlayFactory) overlay._getFactory())._create((V) null, parent, refMgr);
				overlay._setReference(refOverlay);
			}
		} else {
			JsonOverlay existing = refMgr.getRegistry().getOverlay(json, getSignature());
			if (existing != null) {
				overlay = (JsonOverlay) existing;
				if (parent != null) {
					overlay._setParent(parent);
				}
			} else {
				overlay = _create(json, parent, refMgr);
				overlay._setParent(parent);
				refMgr.getRegistry().register(json, getSignature(), overlay);
				if (!overlay._isElaborated()) {
					overlay._elaborate(true);
				}
			}
		}
		return overlay;
	}

	public boolean isCompatible(JsonOverlay overlay) {
		return overlayClass.isAssignableFrom(overlay.getClass());
	}

	public String getSignature() {
		return getOverlayClass().getSimpleName();
	}

	protected boolean isExtendedType() {
		return false;
	}

	protected abstract Class> getOverlayClass();

	protected abstract JsonOverlay _create(V value, JsonOverlay parent, ReferenceManager refMgr);

	protected abstract JsonOverlay _create(JsonNode json, JsonOverlay parent, ReferenceManager refMgr);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy