001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2016 ForgeRock AS.
015 */
016
017package org.forgerock.api.models;
018
019import static org.forgerock.api.util.ValidationUtil.containsWhitespace;
020import static org.forgerock.api.util.ValidationUtil.isEmpty;
021
022import java.util.Objects;
023
024import org.forgerock.api.ApiValidationException;
025
026import com.fasterxml.jackson.annotation.JsonCreator;
027import com.fasterxml.jackson.annotation.JsonValue;
028import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
029
030/**
031 * Class that represents the Reference type in API descriptor.
032 */
033@JsonDeserialize(builder = Reference.Builder.class)
034public final class Reference {
035
036    private final String value;
037
038    private Reference(Builder builder) {
039        this.value = builder.value;
040
041        if (isEmpty(value)) {
042            throw new ApiValidationException("Reference-value is required");
043        }
044        if (containsWhitespace(value)) {
045            throw new ApiValidationException("Reference-value may not contain whitespace");
046        }
047    }
048
049    /**
050     * Getter of the JSON reference.
051     *
052     * @return value
053     */
054    @JsonValue
055    public String getValue() {
056        return value;
057    }
058
059    @Override
060    public boolean equals(Object o) {
061        if (this == o) {
062            return true;
063        }
064        if (o == null || getClass() != o.getClass()) {
065            return false;
066        }
067        Reference reference = (Reference) o;
068        return Objects.equals(value, reference.value);
069    }
070
071    @Override
072    public int hashCode() {
073        return Objects.hash(value);
074    }
075
076    /**
077     * Create a new Builder for Reference.
078     *
079     * @return Builder
080     */
081    public static Builder reference() {
082        return new Builder();
083    }
084
085    /**
086     * Builder to help construct the Reference.
087     */
088    public static final class Builder {
089
090        private String value;
091
092        private Builder() {
093        }
094
095        @JsonCreator
096        private Builder(String value) {
097            this.value = value;
098        }
099
100        /**
101         * Setter for Reference-value.
102         *
103         * @param value Reference-value
104         * @return Builder
105         */
106        @JsonCreator
107        public Builder value(String value) {
108            this.value = value;
109            return this;
110        }
111
112        /**
113         * Builds the Reference instance.
114         *
115         * @return Reference instance
116         */
117        public Reference build() {
118            return new Reference(this);
119        }
120    }
121
122}