guru.nidi.graphviz.attribute.Attributes Maven / Gradle / Ivy
/*
* 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> {
static Attributes attr(String key, @Nullable Object value) {
return new MapAttributes(key, value);
}
static Attributes attrs() {
return new MapAttributes<>();
}
@SafeVarargs
static Attributes attrs(Attributes extends F>... attributes) {
return attrs(asList(attributes));
}
static Attributes attrs(List> attributes) {
final MapAttributes res = new MapAttributes<>();
for (Attributes extends F> attribute : attributes) {
attribute.applyTo(res);
}
return res;
}
Attributes super F> applyTo(MapAttributes super F> attrs);
default Attributes super F> applyTo(Attributes super F> attrs) {
if (!(attrs instanceof MapAttributes)) {
throw new UnsupportedOperationException("attributes must be a MapAttributes");
}
@SuppressWarnings("unchecked") final MapAttributes super F> as = (MapAttributes super F>) attrs;
return applyTo(as);
}
default Attributes copy() {
@SuppressWarnings("unchecked") final Attributes copy = (Attributes) applyTo(attrs());
return copy;
}
@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