1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.upgrade.util;
16  
17  import com.liferay.portal.kernel.upgrade.StagnantRowException;
18  import com.liferay.portal.kernel.upgrade.util.ValueMapper;
19  
20  import java.util.Iterator;
21  import java.util.LinkedHashMap;
22  import java.util.LinkedHashSet;
23  import java.util.Map;
24  import java.util.Set;
25  
26  /**
27   * <a href="MemoryValueMapper.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Alexander Chow
30   * @author Brian Wing Shun Chan
31   */
32  public class MemoryValueMapper implements ValueMapper {
33  
34      public MemoryValueMapper() {
35          this(new LinkedHashSet<Object>());
36      }
37  
38      public MemoryValueMapper(Set<Object> exceptions) {
39          _map = new LinkedHashMap<Object, Object>();
40          _exceptions = exceptions;
41      }
42  
43      public Object getNewValue(Object oldValue) throws Exception {
44          Object value = _map.get(oldValue);
45  
46          if (value == null) {
47              if (_exceptions.contains(oldValue)) {
48                  value = oldValue;
49              }
50              else {
51                  throw new StagnantRowException(String.valueOf(oldValue));
52              }
53          }
54  
55          return value;
56      }
57  
58      public void mapValue(Object oldValue, Object newValue) throws Exception {
59          _map.put(oldValue, newValue);
60      }
61  
62      public void appendException(Object exception) {
63          _exceptions.add(exception);
64      }
65  
66      public Iterator<Object> iterator() throws Exception {
67          return _map.keySet().iterator();
68      }
69  
70      public int size() throws Exception {
71          return _map.size();
72      }
73  
74      public Map<Object, Object> getMap() {
75          return _map;
76      }
77  
78      private Map<Object, Object> _map;
79      private Set<Object> _exceptions;
80  
81  }