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

net.sixpointsix.carpo.common.model.mutable.MutablePropertyCollection Maven / Gradle / Ivy

There is a newer version: 0.7.0
Show newest version
package net.sixpointsix.carpo.common.model.mutable;

import net.sixpointsix.carpo.common.model.Property;
import net.sixpointsix.carpo.common.model.PropertyCollection;

import java.util.*;

public class MutablePropertyCollection extends ArrayList implements PropertyCollection {

    /**
     * Get a property by its key
     *
     * @param key property key
     * @return Property optional, empty if the key is not set
     */
    @Override
    public Optional getByKey(final String key) {
        if(key == null) {
            return Optional.empty();
        }

        return stream()
                .filter(p -> key.equalsIgnoreCase(p.getKey()))
                .findFirst();
    }

    /**
     * Test if a property exists with a key
     *
     * @param key property key
     * @return True if the property exists
     */
    @Override
    public Boolean hasPropertyByKey(final String key) {
        return getByKey(key).isPresent();
    }

    /**
     * Get the string value if it exists
     *
     * 

* This method will get the string value and will return an empty optional if the property with key does not * exist or the value is not a string *

* * @param key property key * @return optional string */ @Override public Optional getStringByKey(final String key) { return getByKey(key).flatMap(Property::getStringValue); } /** * Get the long value if it exists * *

* This method will get the long value and will return an empty optional if the property with key does not * exist or the value is not a long *

* * @param key property key * @return optional long */ @Override public Optional getLongByKey(final String key) { return getByKey(key).flatMap(Property::getLongValue); } /** * Get the double value if it exists * *

* This method will get the double value and will return an empty optional if the property with key does not * exist or the value is not a double *

* * @param key property key * @return optional double */ @Override public Optional getDoubleByKey(final String key) { return getByKey(key).flatMap(Property::getDoubleValue); } /** * Get the boolean value if it exists * *

* This method will get the boolean value and will return an empty optional if the property with key does not * exist or the value is not a boolean *

* * @param key property key * @return optional boolean */ @Override public Optional getBooleanByKey(final String key) { return getByKey(key).flatMap(Property::getBooleanValue); } /** * Get the object value if it exists * *

* This method will get the object value and will return an empty optional if the property with key does not * exist or the value is not a object or it cannot be cast to T *

* * @param key property key * @param type type to cast to * @return optional object */ @Override public Optional getObjectByKey(final String key, final Class type) { return getByKey(key).flatMap(o -> o.getObjectValue(type)); } /** * Get the object values as a list * *

* This method will get the list of object values and will return an empty list if the property with key does * not exist. Each element will be cast a T and only the successful ones will be returned *

* * @param key property key * @param type type to cast to * @return list of object */ @Override public List getListByKey(String key, Class type) { return getByKey(key).map(o -> o.getListValue(type)).orElse(List.of()); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy