001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper.definitions;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018
019import org.basepom.mojo.propertyhelper.FieldContext;
020import org.basepom.mojo.propertyhelper.ValueCache;
021import org.basepom.mojo.propertyhelper.ValueProvider;
022import org.basepom.mojo.propertyhelper.fields.UuidField;
023
024import java.io.IOException;
025import java.util.Objects;
026import java.util.Optional;
027import java.util.StringJoiner;
028import java.util.UUID;
029
030import com.google.common.annotations.VisibleForTesting;
031
032public class UuidDefinition extends FieldDefinition<String> {
033
034    /**
035     * Value for this uuid. Field injected by Maven.
036     */
037    String value = null;
038
039    public UuidDefinition() {}
040
041    @VisibleForTesting
042    UuidDefinition(String id) {
043        super(id);
044    }
045
046    public Optional<UUID> getValue() {
047        return Optional.ofNullable(value).map(UUID::fromString);
048    }
049
050    @Override
051    public UuidField createField(FieldContext context, ValueCache valueCache) throws IOException {
052        checkNotNull(context, "context is null");
053        checkNotNull(valueCache, "valueCache is null");
054
055        check();
056
057        final ValueProvider uuidValue = valueCache.getValueProvider(this);
058        return new UuidField(this, uuidValue, context);
059    }
060
061    @Override
062    public String toString() {
063        return new StringJoiner(", ", UuidDefinition.class.getSimpleName() + "[", "]")
064            .add("value='" + value + "'")
065            .add(super.toString())
066            .toString();
067    }
068
069    @Override
070    public boolean equals(Object o) {
071        if (this == o) {
072            return true;
073        }
074        if (o == null || getClass() != o.getClass()) {
075            return false;
076        }
077        if (!super.equals(o)) {
078            return false;
079        }
080        UuidDefinition that = (UuidDefinition) o;
081        return Objects.equals(value, that.value);
082    }
083
084    @Override
085    public int hashCode() {
086        return Objects.hash(super.hashCode(), value);
087    }
088}