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.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    }