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

org.xmlobjects.gml.model.common.LocalProperties Maven / Gradle / Ivy

There is a newer version: 1.1.2
Show newest version
/*
 * gml-objects - A Java mapping for the OGC Geography Markup Language (GML)
 * https://github.com/xmlobjects/gml-objects
 *
 * Copyright 2019-2024 Claus Nagel 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xmlobjects.gml.model.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

public class LocalProperties implements Serializable {
    private Map properties;

    public Object get(String name) {
        return properties != null ? properties.get(name) : null;
    }

    public  T get(String name, Class type) {
        if (properties != null) {
            Object value = properties.get(name);
            return value != null && type.isAssignableFrom(value.getClass()) ? type.cast(value) : null;
        } else
            return null;
    }

    public boolean getAndCompare(String name, Object expectedValue) {
        return Objects.equals(get(name), expectedValue);
    }

    public  T getOrDefault(String name, Class type, Supplier supplier) {
        T value = get(name, type);
        return value != null ? value : supplier.get();
    }

    public  T getOrSet(String name, Class type, Supplier supplier) {
        T value = get(name, type);
        if (value == null) {
            value = supplier.get();
            set(name, value);
        }

        return value;
    }

    public boolean contains(String name) {
        return properties != null && properties.containsKey(name);
    }

    public void set(String name, Object value) {
        if (properties == null)
            properties = new HashMap<>();

        properties.put(name, value);
    }

    public boolean isEmpty() {
        return properties == null || properties.isEmpty();
    }

    public void remove(String name) {
        if (properties != null)
            properties.remove(name);
    }

    public void clear() {
        if (properties != null)
            properties.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy