001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.tridentsdk.meta.component;
018
019import com.google.common.collect.ImmutableMap;
020import jdk.nashorn.internal.ir.annotations.Immutable;
021
022import java.util.Map;
023import java.util.function.Consumer;
024
025/**
026 * Represents an immutable view of a metacollection
027 *
028 * @author The TridentSDK Team
029 * @since 0.4-alpha
030 */
031@Immutable
032public class ImmutableMetaCollection<S> implements MetaCollection<S> {
033    private final Map<Class<? extends Meta<S>>, Meta<S>> map;
034
035    private ImmutableMetaCollection(ImmutableMap<Class<? extends Meta<S>>, Meta<S>> map) {
036        this.map = map;
037    }
038
039    public static <T extends MetaOwner> MetaCollection<T> copyOf(MetaCollection<T> collection) {
040        ImmutableMap.Builder<Class<? extends Meta<T>>, Meta<T>> b = ImmutableMap.builder();
041        collection.iterate((e) -> b.put(e.getKey(), e.getValue()));
042        return new ImmutableMetaCollection<>(b.build());
043    }
044
045    @Override
046    public <T extends Meta<S>> T get(Class<T> cls) {
047        return (T) map.get(cls);
048    }
049
050    @Override
051    public <T extends Meta<S>> void put(T meta) {
052        throw new UnsupportedOperationException();
053    }
054
055    @Override
056    public <T extends Meta<S>> boolean putIfAbsent(T meta) {
057        throw new UnsupportedOperationException();
058    }
059
060    @Override
061    public <T extends Meta<S>> void put(Class<T> cls, T meta) {
062        throw new UnsupportedOperationException();
063    }
064
065    @Override
066    public <T extends Meta<S>> boolean putIfAbsent(Class<T> cls, T meta) {
067        throw new UnsupportedOperationException();
068    }
069
070    @Override
071    public <T extends Meta<S>> boolean contains(Class<T> cls) {
072        return map.containsKey(cls);
073    }
074
075    @Override
076    public <T extends Meta<S>> T remove(Class<T> cls) {
077        throw new UnsupportedOperationException();
078    }
079
080    @Override
081    public void iterate(Consumer<Map.Entry<Class<? extends Meta<S>>, Meta<S>>> consumer) {
082        for (Map.Entry<Class<? extends Meta<S>>, Meta<S>> e : map.entrySet()) {
083            consumer.accept(e);
084        }
085    }
086
087    @Override
088    public void clear() {
089        throw new UnsupportedOperationException();
090    }
091}