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

org.xlcloud.service.heat.parsers.utils.UnrecognizedPropertiesDecorator Maven / Gradle / Ivy

/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.service.heat.parsers.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xlcloud.service.heat.parsers.utils.iterable.JsonPair;
import org.xlcloud.service.heat.template.commons.HeatTemplateValue;
import org.xlcloud.service.heat.template.fields.JsonKey;

/**
 * This class decorates {@link JsonObjectWrapper}, adding the ability to
 * retrieve unrecognized (not accessed) properties.
 * 
 * @author Krzysztof Szafrański, AMG.net
 */
public class UnrecognizedPropertiesDecorator implements JsonObjectWrapper {

    private JsonObjectWrapper wrapper;
    
    private final Set accessedKeys = new HashSet<>();
    
    public static UnrecognizedPropertiesDecorator decorate(JsonObjectWrapper jsonObjectWrapper) {
        return new UnrecognizedPropertiesDecorator(jsonObjectWrapper);
    }

    private UnrecognizedPropertiesDecorator( JsonObjectWrapper wrapper ) {
        this.wrapper = wrapper;
    }

    @Override
    public Iterator iterator() {
        return wrapper.iterator();
    }

    @Override
    public JSONObject toJsonObject() {
        return wrapper.toJsonObject();
    }

    @Override
    public JsonObjectWrapper getWrapper(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.getWrapper(key);
    }

    @Override
    public JSONObject get(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.get(key);
    }

    @Override
    public String getString(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.getString(key);
    }

    @Override
    public String optStringNull(String key) {
        accessedKeys.add(key);
        return wrapper.optStringNull(key);
    }

    @Override
    public String optStringNull(JsonKey key) {
        accessedKeys.add(key.jsonKey());
        return wrapper.optStringNull(key);
    }

    @Override
    public JSONArray getArray(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.getArray(key);
    }

    @Override
    public List requiredArray(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.requiredArray(key);
    }

    @Override
    public List optArrayNull(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.optArrayNull(key);
    }

    @Override
    public Boolean optBooleanNull(JsonKey key) {
        accessedKeys.add(key.jsonKey());
        return wrapper.optBooleanNull(key);
    }

    @Override
    public Long optLongNull(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.optLongNull(key);
    }

    @Override
    public Integer optIntegerNull(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.optIntegerNull(key);
    }

    @Override
    public JsonObjectWrapper put(JsonKey key, JSONObject jsonSnippet) throws JSONException {
        wrapper.put(key, jsonSnippet);
        return this;
    }

    @Override
    public JsonObjectWrapper putOpt(JsonKey key, JSONObject jsonSnippet) throws JSONException {
        wrapper.putOpt(key, jsonSnippet);
        return this;
    }

    @Override
    public JsonObjectWrapper putOpt(JsonKey key, Boolean value) throws JSONException {
        wrapper.putOpt(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putOpt(JsonKey key, String value) throws JSONException {
        wrapper.putOpt(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putOpt(JsonKey key, Integer value) throws JSONException {
        wrapper.putOpt(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putOptHeatTemplateValue(JsonKey key, HeatTemplateValue value) throws JSONException {
        wrapper.putOptHeatTemplateValue(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putHeatTemplateValue(JsonKey key, HeatTemplateValue value) throws JSONException {
        wrapper.putHeatTemplateValue(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper put(JsonKey key, Object jsonSnippet) throws JSONException {
        wrapper.put(key, jsonSnippet);
        return this;
    }

    @Override
    public JsonObjectWrapper put(JsonKey key, JsonObjectWrapper jsonSnippet) throws JSONException {
        wrapper.put(key, jsonSnippet);
        return this;
    }

    @Override
    public JsonObjectWrapper put(JsonKey key, String string) throws JSONException {
        wrapper.put(key, string);
        return this;
    }

    @Override
    public JsonObjectWrapper putValue(JsonKey key, String value) throws JSONException {
        wrapper.put(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putValue(JsonKey key, Boolean value) throws JSONException {
        wrapper.put(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putValueOrEmptyString(JsonKey key, String value) throws JSONException {
        wrapper.put(key, value);
        return this;
    }

    @Override
    public JsonObjectWrapper putValue(JsonKey key, Collection array) throws JSONException {
        wrapper.put(key, array);
        return this;
    }

    @Override
    public boolean has(JsonKey key) {
        return wrapper.has(key);
    }

    @Override
    public boolean hasNot(JsonKey key) {
        return wrapper.hasNot(key);
    }

    @Override
    public HeatTemplateValue optHeatTemplateValueNull(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.optHeatTemplateValueNull(key);
    }

    @Override
    public HeatTemplateValue getHeatTemplateValue(JsonKey key) throws JSONException {
        accessedKeys.add(key.jsonKey());
        return wrapper.getHeatTemplateValue(key);
    }
    
    /**
     * Returns a list of all properties that have not been accessed.
     * @return
     */
    public List getUnrecognizedProperties() {
        List unrecognizedProperties = new ArrayList<>();
        for (JsonPair pair : this) {
            if (!accessedKeys.contains(pair.getKey())) {
                unrecognizedProperties.add(pair);
            }
        }
        return unrecognizedProperties;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy