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

org.flips.model.FlipAnnotationAttributes Maven / Gradle / Ivy

Go to download

Flips Core framework, provides all the flip annotations, conditions and different advices

There is a newer version: 1.1
Show newest version
package org.flips.model;

import org.flips.utils.ValidationUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class FlipAnnotationAttributes {

    private Map attributes = new HashMap<>();

    private FlipAnnotationAttributes(Map attributes) {
        this.attributes = attributes;
    }

    private Optional getAttributeValue(String attributeName) {
        ValidationUtils.requireNonEmpty(attributeName, "attributeName can not be NULL or EMPTY");
        return Optional.ofNullable(attributes.get(attributeName));
    }

    public  T getAttributeValue(String attributeName, T defaultValue){
        return (T) getAttributeValue(attributeName).orElse(defaultValue);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        FlipAnnotationAttributes that = (FlipAnnotationAttributes) o;
        return attributes.equals(that.attributes);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("FlipAnnotationAttributes{");
        sb.append("attributes=").append(attributes);
        sb.append('}');
        return sb.toString();
    }

    public static class Builder{

        private Map attributes = new HashMap<>();

        public Builder addAll(Map newAttributes){
            ValidationUtils.requireNonNull(newAttributes, "attributes to be added in FlipAnnotationAttributes can not be null");
            attributes.putAll(new HashMap<>(newAttributes));
            return this;
        }

        public FlipAnnotationAttributes build(){
            return new FlipAnnotationAttributes(attributes);
        }
    }
}