001package org.cache2k.configuration;
002
003/*
004 * #%L
005 * cache2k API
006 * %%
007 * Copyright (C) 2000 - 2016 headissue GmbH, Munich
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.ArrayList;
024import java.util.Collection;
025import java.util.Iterator;
026
027/**
028 * Default implementation for a customization collection using a list.
029 *
030 * <p>Rationale: Inserting is a little expansive, since we check whether
031 * an entry is already existing. Since there are usually only a few entries
032 * the list is sufficient.
033 *
034 * @author Jens Wilke
035 */
036public class DefaultCustomizationCollection<T> implements CustomizationCollection<T> {
037
038  private Collection<CustomizationSupplier<T>> list = new ArrayList<CustomizationSupplier<T>>();
039
040  @Override
041  public int size() {
042    return list.size();
043  }
044
045  @Override
046  public boolean isEmpty() {
047    return list.isEmpty();
048  }
049
050  @Override
051  public boolean contains(final Object o) {
052    return list.contains(o);
053  }
054
055  @Override
056  public Iterator<CustomizationSupplier<T>> iterator() {
057    return list.iterator();
058  }
059
060  @Override
061  public Object[] toArray() {
062    return list.toArray();
063  }
064
065  @Override
066  public <T> T[] toArray(final T[] a) {
067    return list.toArray(a);
068  }
069
070  /**
071   * Adds a customization to the collection.
072   *
073   * @return alsways {@code true}
074   * @throws IllegalArgumentException if the entry is already existing.
075   */
076  @Override
077  public boolean add(final CustomizationSupplier<T> entry) {
078    if (list.contains(entry)) {
079      throw new IllegalArgumentException("duplicate entry");
080    }
081    return list.add(entry);
082  }
083
084  @Override
085  public boolean remove(final Object o) {
086    return list.remove(o);
087  }
088
089  @Override
090  public boolean containsAll(final Collection<?> c) {
091    return list.containsAll(c);
092  }
093
094  @Override
095  public boolean addAll(final Collection<? extends CustomizationSupplier<T>> c) {
096    for (CustomizationSupplier<T> e : c) {
097      add(e);
098    }
099    return true;
100  }
101
102  @Override
103  public boolean removeAll(final Collection<?> c) {
104    return list.removeAll(c);
105  }
106
107  @Override
108  public boolean retainAll(final Collection<?> c) {
109    return list.retainAll(c);
110  }
111
112  @Override
113  public void clear() {
114    list.clear();
115  }
116
117}