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

guru.nidi.graphviz.attribute.Attributes Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Copyright © 2015 Stefan Niederhauser ([email protected])
 *
 * 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 guru.nidi.graphviz.attribute;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import static java.util.Arrays.asList;

public interface Attributes extends Iterable> {
    Attributes applyTo(MapAttributes attrs);

    default Attributes applyTo(Attributes attrs) {
        if (!(attrs instanceof MapAttributes)) {
            throw new UnsupportedOperationException("attributes must be a MapAttributes");
        }
        @SuppressWarnings("unchecked") final MapAttributes as = (MapAttributes) attrs;
        return applyTo(as);
    }

    default Attributes copy() {
        @SuppressWarnings("unchecked") final Attributes copy = (Attributes) applyTo(attrs());
        return copy;
    }

    static  Attributes attr(String key, @Nullable Object value) {
        return new MapAttributes().add(key, value);
    }

    static  Attributes attrs() {
        return new MapAttributes<>();
    }

    @SafeVarargs
    static  Attributes attrs(Attributes... attributes) {
        return attrs(asList(attributes));
    }

    static  Attributes attrs(List> attributes) {
        final MapAttributes res = new MapAttributes<>();
        for (Attributes attribute : attributes) {
            attribute.applyTo(res);
        }
        return res;
    }

    @Nullable
    default Object get(String key) {
        return applyTo(new MapAttributes<>()).get(key);
    }

    @Override
    default Iterator> iterator() {
        return applyTo(new MapAttributes<>()).iterator();
    }

    default boolean isEmpty() {
        return applyTo(new MapAttributes<>()).isEmpty();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy