001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.upgrade.util;
016    
017    import com.liferay.portal.kernel.upgrade.StagnantRowException;
018    import com.liferay.portal.kernel.upgrade.util.ValueMapper;
019    
020    import java.util.Iterator;
021    import java.util.LinkedHashMap;
022    import java.util.LinkedHashSet;
023    import java.util.Map;
024    import java.util.Set;
025    
026    /**
027     * @author Alexander Chow
028     * @author Brian Wing Shun Chan
029     */
030    public class MemoryValueMapper implements ValueMapper {
031    
032            public MemoryValueMapper() {
033                    this(new LinkedHashSet<Object>());
034            }
035    
036            public MemoryValueMapper(Set<Object> exceptions) {
037                    _map = new LinkedHashMap<Object, Object>();
038                    _exceptions = exceptions;
039            }
040    
041            public void appendException(Object exception) {
042                    _exceptions.add(exception);
043            }
044    
045            public Map<Object, Object> getMap() {
046                    return _map;
047            }
048    
049            public Object getNewValue(Object oldValue) throws Exception {
050                    Object value = _map.get(oldValue);
051    
052                    if (value == null) {
053                            if (_exceptions.contains(oldValue)) {
054                                    value = oldValue;
055                            }
056                            else {
057                                    throw new StagnantRowException(String.valueOf(oldValue));
058                            }
059                    }
060    
061                    return value;
062            }
063    
064            public Iterator<Object> iterator() throws Exception {
065                    return _map.keySet().iterator();
066            }
067    
068            public void mapValue(Object oldValue, Object newValue) throws Exception {
069                    _map.put(oldValue, newValue);
070            }
071    
072            public int size() throws Exception {
073                    return _map.size();
074            }
075    
076            private Set<Object> _exceptions;
077            private Map<Object, Object> _map;
078    
079    }