1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.upgrade.util;
21  
22  import com.liferay.portal.upgrade.StagnantRowException;
23  
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.LinkedHashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  /**
31   * <a href="MemoryValueMapper.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Alexander Chow
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class MemoryValueMapper implements ValueMapper {
38  
39      public MemoryValueMapper() {
40          this(new LinkedHashSet<Object>());
41      }
42  
43      public MemoryValueMapper(Set<Object> exceptions) {
44          _map = new LinkedHashMap<Object, Object>();
45          _exceptions = exceptions;
46      }
47  
48      public Object getNewValue(Object oldValue) throws Exception {
49          Object value = _map.get(oldValue);
50  
51          if (value == null) {
52              if (_exceptions.contains(oldValue)) {
53                  value = oldValue;
54              }
55              else {
56                  throw new StagnantRowException(String.valueOf(oldValue));
57              }
58          }
59  
60          return value;
61      }
62  
63      public void mapValue(Object oldValue, Object newValue) throws Exception {
64          _map.put(oldValue, newValue);
65      }
66  
67      public void appendException(Object exception) {
68          _exceptions.add(exception);
69      }
70  
71      public Iterator<Object> iterator() throws Exception {
72          return _map.keySet().iterator();
73      }
74  
75      public int size() throws Exception {
76          return _map.size();
77      }
78  
79      public Map<Object, Object> getMap() {
80          return _map;
81      }
82  
83      private Map<Object, Object> _map;
84      private Set<Object> _exceptions;
85  
86  }