com.synflow.cx.internal.instantiation.properties.PropertiesChecker.java Source code

Java tutorial

Introduction

Here is the source code for com.synflow.cx.internal.instantiation.properties.PropertiesChecker.java

Source

/*******************************************************************************
 * Copyright (c) 2013-2014 Synflow SAS.
 * 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:
 *    Matthieu Wipliez - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.synflow.cx.internal.instantiation.properties;

import static com.synflow.core.IProperties.PROP_CLOCKS;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * This class defines an abstract properties checker that is subclassed by concrete implementations
 * to check entities and instances respectively.
 * 
 * @author Matthieu Wipliez
 * 
 */
public abstract class PropertiesChecker {

    /**
     * {clock: 'name'} accepted as synonym for {clocks: ['name']}
     */
    private static final String PROP_CLOCK = "clock";

    protected final IJsonErrorHandler handler;

    protected PropertiesChecker(IJsonErrorHandler handler) {
        this.handler = handler;
    }

    /**
     * If the given properties define {clock: 'name'}, and no 'clocks' property, transform to
     * {clocks: ['name']}.
     * 
     * @param properties
     */
    protected final void applyClockShortcut(JsonObject properties) {
        JsonElement clock = properties.get(PROP_CLOCK);
        if (clock == null) {
            // no clock property, return
            return;
        }

        if (properties.has(PROP_CLOCKS)) {
            // both 'clock' and 'clocks' exist, show error and ignore 'clock'
            handler.addError(clock, "'clock' and 'clocks' are mutually exclusive");
            return;
        }

        if (clock.isJsonNull()) {
            // {clock: null} becomes {clocks: []}
            properties.add(PROP_CLOCKS, new JsonArray());
        } else if (clock.isJsonPrimitive() && clock.getAsJsonPrimitive().isString()) {
            // clock is valid, {clock: name} becomes {clocks: [name]}
            JsonArray clocksArray = new JsonArray();
            clocksArray.add(clock);
            properties.add(PROP_CLOCKS, clocksArray);
        } else {
            // clock not valid, ignore
            handler.addError(clock, "'clock' must be a valid clock name");
        }
    }

    /**
     * Check the 'clocks 'array.
     * 
     * @param clocks
     *            an array of clock names
     * @return <code>true</code> if it is valid
     */
    protected boolean checkClockArray(JsonElement clocks) {
        boolean isValid;
        if (clocks.isJsonArray()) {
            isValid = true;
            JsonArray clocksArray = clocks.getAsJsonArray();
            for (JsonElement clock : clocksArray) {
                if (!clock.isJsonPrimitive() || !clock.getAsJsonPrimitive().isString()) {
                    isValid = false;
                    break;
                }
            }
        } else {
            isValid = false;
        }

        if (!isValid) {
            handler.addError(clocks, "'clocks' must be an array of clock names");
        }
        return isValid;
    }

}