001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.velocity;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.Enumeration;
027    import java.util.Map;
028    import java.util.Set;
029    import java.util.concurrent.ConcurrentHashMap;
030    
031    import org.apache.commons.collections.ExtendedProperties;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class FastExtendedProperties extends ExtendedProperties {
037    
038            public FastExtendedProperties() {
039            }
040    
041            public FastExtendedProperties(ExtendedProperties extendedProperties)
042                    throws IOException {
043    
044                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
045                            new UnsyncByteArrayOutputStream();
046    
047                    extendedProperties.save(unsyncByteArrayOutputStream, StringPool.BLANK);
048    
049                    InputStream inputStream = new UnsyncByteArrayInputStream(
050                            unsyncByteArrayOutputStream.unsafeGetByteArray(), 0,
051                            unsyncByteArrayOutputStream.size());
052    
053                    load(inputStream);
054            }
055    
056            @Override
057            public void clear() {
058                    _map.clear();
059            }
060    
061            @Override
062            public Object clone() {
063                    FastExtendedProperties fastExtendedProperties =
064                            (FastExtendedProperties)super.clone();
065    
066                    fastExtendedProperties._map = new ConcurrentHashMap<Object, Object>(
067                            _map);
068    
069                    return fastExtendedProperties;
070            }
071    
072            @Override
073            public boolean contains(Object value) {
074                    return _map.containsKey(value);
075            }
076    
077            @Override
078            public boolean containsKey(Object key) {
079                    return _map.containsKey(key);
080            }
081    
082            @Override
083            public boolean containsValue(Object value) {
084                    return _map.containsValue(value);
085            }
086    
087            @Override
088            @SuppressWarnings("rawtypes")
089            public Enumeration elements() {
090                    return Collections.enumeration(_map.values());
091            }
092    
093            @Override
094            @SuppressWarnings("rawtypes")
095            public Set entrySet() {
096                    return _map.entrySet();
097            }
098    
099            @Override
100            public boolean equals(Object o) {
101                    return _map.equals(o);
102            }
103    
104            @Override
105            public Object get(Object key) {
106                    return _map.get(key);
107            }
108    
109            @Override
110            public int hashCode() {
111                    return _map.hashCode();
112            }
113    
114            @Override
115            public boolean isEmpty() {
116                    return _map.isEmpty();
117            }
118    
119            @Override
120            @SuppressWarnings("rawtypes")
121            public Enumeration keys() {
122                    return Collections.enumeration(_map.keySet());
123            }
124    
125            @Override
126            @SuppressWarnings("rawtypes")
127            public Set keySet() {
128                    return _map.keySet();
129            }
130    
131            @Override
132            public Object put(Object key, Object value) {
133                    return _map.put(key, value);
134            }
135    
136            @Override
137            @SuppressWarnings("rawtypes")
138            public void putAll(Map t) {
139                    _map.putAll(t);
140            }
141    
142            @Override
143            public Object remove(Object key) {
144                    return _map.remove(key);
145            }
146    
147            @Override
148            public int size() {
149                    return _map.size();
150            }
151    
152            @Override
153            public String toString() {
154                    return _map.toString();
155            }
156    
157            @Override
158            @SuppressWarnings("rawtypes")
159            public Collection values() {
160                    return _map.values();
161            }
162    
163            private Map<Object, Object> _map = new ConcurrentHashMap<Object, Object>();
164    
165    }