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

com.glookast.commons.base.PropertyList Maven / Gradle / Ivy

package com.glookast.commons.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PropertyList", namespace = "http://base.commons.glookast.com", propOrder = {
    "entry"
})
public class PropertyList implements Serializable
{
    protected final List entry;

    public PropertyList()
    {
        entry = new ArrayList<>();
    }

    public PropertyList(PropertyList propertyList)
    {
        entry = new ArrayList<>();
        for (Entry e : propertyList.entry) {
            entry.add(new Entry(e));
        }
    }

    public List getEntry()
    {
        return entry;
    }

    public void add(String key, String... value)
    {
        for (String v : value) {
            entry.add(new Entry(key, v));
        }
    }

    public void put(String key, String... value)
    {
        remove(key);
        add(key, value);
    }

    public void add(String key, int... value)
    {
        for (int v : value) {
            entry.add(new Entry(key, String.valueOf(v)));
        }
    }

    public void put(String key, int... value)
    {
        remove(key);
        add(key, value);
    }

    public void add(String key, long... value)
    {
        for (long v : value) {
            entry.add(new Entry(key, String.valueOf(v)));
        }
    }

    public void put(String key, long... value)
    {
        remove(key);
        add(key, value);
    }

    public void add(String key, UUID... value)
    {
        for (UUID v : value) {
            entry.add(new Entry(key, String.valueOf(v)));
        }
    }

    public void put(String key, UUID... value)
    {
        remove(key);
        add(key, value);
    }

    public boolean containsKey(String key)
    {
        for (Entry e : entry) {
            if (Objects.equals(e.getKey(), key)) {
                return true;
            }
        }
        return false;
    }

    public void remove(String key)
    {
        for (Iterator it = entry.iterator(); it.hasNext();) {
            Entry e = it.next();
            if (Objects.equals(e.getKey(), key)) {
                it.remove();
            }
        }
    }

    public String getString(String key, String defaultValue)
    {
        for (Entry e : entry) {
            if (Objects.equals(e.getKey(), key)) {
                return e.getValue();
            }
        }
        return defaultValue;
    }

    public String getString(String key)
    {
        return getString(key, null);
    }

    public int getInt(String key, int defaultValue)
    {
        try {
            for (Entry e : entry) {
                if (Objects.equals(e.getKey(), key)) {
                    return Integer.valueOf(e.getValue());
                }
            }
        } catch (Exception ex) {
        }
        return defaultValue;
    }

    public int getInt(String key)
    {
        return getInt(key, 0);
    }

    public long getLong(String key, long defaultValue)
    {
        try {
            for (Entry e : entry) {
                if (Objects.equals(e.getKey(), key)) {
                    return Long.valueOf(e.getValue());
                }
            }
        } catch (Exception ex) {
        }
        return defaultValue;
    }

    public long getLong(String key)
    {
        return getLong(key, 0);
    }

    public UUID getUUID(String key, UUID defaultValue)
    {
        try {
            for (Entry e : entry) {
                if (Objects.equals(e.getKey(), key)) {
                    return UUID.fromString(e.getValue());
                }
            }
        } catch (Exception ex) {
        }
        return defaultValue;
    }

    public UUID getUUID(String key)
    {
        return getUUID(key, null);
    }

    public List getAll(String key)
    {
        List list = new ArrayList<>();
        for (Entry e : entry) {
            if (Objects.equals(e.getKey(), key)) {
                list.add(e.getValue());
            }
        }
        return list;
    }

    public void clear()
    {
        entry.clear();
    }

    @Override
    public String toString()
    {
        return "PropertyList{" + "entry=" + entry + '}';
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "key",
        "value"
    })
    public static class Entry implements Serializable
    {
        @XmlElement(required = true)
        protected String key;
        @XmlElement(required = true)
        protected String value;

        public Entry()
        {
        }

        public Entry(Entry entry)
        {
            this.key = entry.key;
            this.value = entry.value;
        }

        public Entry(String key, String value)
        {
            this.key = key;
            this.value = value;
        }

        public String getKey()
        {
            return key;
        }

        public void setKey(String value)
        {
            this.key = value;
        }

        public String getValue()
        {
            return value;
        }

        public void setValue(String value)
        {
            this.value = value;
        }

        @Override
        public String toString()
        {
            return "Entry{" + "key=" + key + ", value=" + value + '}';
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy