001    /**
002     * Copyright (c) 2000-2013 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            @Override
042            public void appendException(Object exception) {
043                    _exceptions.add(exception);
044            }
045    
046            public Map<Object, Object> getMap() {
047                    return _map;
048            }
049    
050            @Override
051            public Object getNewValue(Object oldValue) throws Exception {
052                    Object value = _map.get(oldValue);
053    
054                    if (value == null) {
055                            if (_exceptions.contains(oldValue)) {
056                                    value = oldValue;
057                            }
058                            else {
059                                    throw new StagnantRowException(String.valueOf(oldValue));
060                            }
061                    }
062    
063                    return value;
064            }
065    
066            @Override
067            public Iterator<Object> iterator() throws Exception {
068                    return _map.keySet().iterator();
069            }
070    
071            @Override
072            public void mapValue(Object oldValue, Object newValue) throws Exception {
073                    _map.put(oldValue, newValue);
074            }
075    
076            @Override
077            public int size() throws Exception {
078                    return _map.size();
079            }
080    
081            private Set<Object> _exceptions;
082            private Map<Object, Object> _map;
083    
084    }