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

com.zuunr.forms.formfield.options.Option Maven / Gradle / Ivy

/*
 * Copyright 2018-2019 Zuunr AB
 *
 * 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 com.zuunr.forms.formfield.options;

import com.zuunr.forms.ValueFormat;
import com.zuunr.json.JsonArray;
import com.zuunr.json.JsonObject;
import com.zuunr.json.JsonObjectSupport;
import com.zuunr.json.JsonValue;

/**
 * @author Niklas Eldberger
 */
public final class Option implements JsonObjectSupport {

    private String label;
    private ValueFormat valueFormat;
    private JsonValue value;
    private JsonObject jsonObject;
    private ValidationSteps validationSteps;

    private Option(JsonValue jsonValue) {

        JsonArray validationStepsJsonArray = jsonValue.get("validationSteps", JsonValue.NULL).getValue(JsonArray.class);

        valueFormat = jsonValue.get("format", JsonValue.NULL).as(ValueFormat.class);
        value = jsonValue.get("value");
        label = jsonValue.get("label", JsonValue.NULL).getValue(String.class);


        jsonObject = JsonObject.EMPTY;
        jsonObject = valueFormat == null ? jsonObject : jsonObject.put("format", valueFormat);
        jsonObject = value == null ? jsonObject : jsonObject.put("value", value);
        jsonObject = label == null ? jsonObject : jsonObject.put("label", label);

        if (validationStepsJsonArray != null) {
            if (valueFormat != null) {
                validationStepsJsonArray = validationStepsJsonArray.add(JsonObject.EMPTY.put("format", valueFormat));
            }
            validationSteps = validationStepsJsonArray.as(ValidationSteps.class);
            jsonObject = jsonObject.put("validationSteps", validationSteps);
        }
    }

    public String label() {
        return label;
    }

    public ValueFormat format() {
        return valueFormat;
    }

    public JsonValue value() {
        return value;
    }

    @Override
    public JsonObject asJsonObject() {
        return jsonObject;
    }

    @Override
    public JsonValue asJsonValue() {
        return jsonObject.jsonValue();
    }

    @Override
    public boolean equals(Object object) {
        return  object != null && object.getClass() == getClass() && ((Option) object).asJsonObject().equals(jsonObject);
    }

    public ValidationSteps validationSteps(){
        return validationSteps;
    }

    public static Builder builder() {
        return new Builder(JsonObject.EMPTY);
    }

    public static final class Builder {

        JsonObject.JsonObjectBuilder jsonObjectBuilder;

        private Builder(JsonObject jsonObject) {
            jsonObjectBuilder = jsonObject.builder();
        }


        public Builder label(String label) {
            jsonObjectBuilder.put("label", label);
            return this;
        }

        public Builder format(ValueFormat valueFormat) {
            jsonObjectBuilder.put("format", valueFormat);
            return this;
        }

        public Builder validationSteps(ValidationSteps validationSteps) {
            jsonObjectBuilder.put("validationSteps", validationSteps);
            return this;
        }

        public Builder value(JsonValue value) {
            jsonObjectBuilder.put("value", value);
            return this;
        }

        public Option build() {

            return jsonObjectBuilder.build().as(Option.class);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy