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 */
017
018package net.tridentsdk.config;
019
020import com.google.gson.JsonArray;
021
022import javax.annotation.concurrent.ThreadSafe;
023import java.util.Collection;
024import java.util.concurrent.locks.Lock;
025
026/**
027 * Section of the config dedicated to storing values from a collection
028 *
029 * @author The TridentSDK Team
030 * @since 0.3-alpha-DP
031 */
032@ThreadSafe
033public class ConfigSectionList<V> extends ConfigList<V> {
034    private final ConfigSection parent;
035
036    private final Lock lock = super.write;
037
038    /**
039     * Creates a new section list in the config
040     *
041     * @param parent the parent configuration section, usually a {@link Config}.
042     * @param handle the array handler
043     */
044    protected ConfigSectionList(ConfigSection parent, JsonArray handle) {
045        super(handle);
046        this.parent = parent;
047    }
048
049    @Override
050    public boolean add(V element) {
051        boolean changed = super.add(element);
052
053        if (changed) {
054            lock.lock();
055            try {
056                this.jsonHandle.add(((ConfigSection) element).asJsonObject());
057            } finally {
058                lock.unlock();
059            }
060        }
061
062        return changed;
063    }
064
065    @Override
066    public boolean addAll(Collection<? extends V> coll) {
067        boolean changed = super.addAll(coll);
068
069        if (changed) {
070            lock.lock();
071            try {
072                for (V element : coll) {
073                    this.jsonHandle.add(((ConfigSection) element).asJsonObject());
074                }
075            } finally {
076                lock.unlock();
077            }
078        }
079
080        return changed;
081    }
082
083    /* (non-Javadoc)
084     * @see java.util.ArrayList#set(int, java.lang.Object)
085     */
086    @Override
087    public V set(int index, V element) {
088        lock.lock();
089        try {
090            this.jsonHandle.set(index, ((ConfigSection) element).asJsonObject());
091            return super.set(index, element);
092        } finally {
093            lock.unlock();
094        }
095    }
096
097    @Override
098    public V remove(int index) {
099        lock.lock();
100        try {
101            this.jsonHandle.remove(index);
102            return super.remove(index);
103        } finally {
104            lock.unlock();
105        }
106    }
107
108    @Override
109    public boolean remove(Object element) {
110        boolean success = super.remove(element);
111
112        if (success) {
113            lock.lock();
114            try {
115                this.jsonHandle.remove(((ConfigSection) element).asJsonObject());
116            } finally {
117                lock.unlock();
118            }
119        }
120
121        return success;
122    }
123
124    /* (non-Javadoc)
125     * @see java.util.ArrayList#removeAll(java.util.Collection)
126     */
127    @Override
128    public boolean removeAll(Collection<?> coll) {
129        boolean changed = super.removeAll(coll);
130
131        if (changed) {
132            for (Object o : coll) {
133                lock.lock();
134                try {
135                    this.jsonHandle.remove(((ConfigSection) o).asJsonObject());
136                } finally {
137                    lock.unlock();
138                }
139            }
140        }
141
142        return changed;
143    }
144
145    /* (non-Javadoc)
146     * @see java.util.ArrayList#clear()
147     */
148    @Override
149    public void clear() {
150        lock.lock();
151        try {
152            super.clear();
153            this.jsonHandle = new JsonArray();
154        } finally {
155            lock.unlock();
156        }
157    }
158
159    protected ConfigSection parent() {
160        return this.parent;
161    }
162}