1 | /* |
2 | * Copyright 2003,2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.apache.pluto.driver.core; |
17 | |
18 | import java.io.IOException; |
19 | import java.io.ObjectInputStream; |
20 | import java.io.ObjectOutputStream; |
21 | import java.io.Serializable; |
22 | |
23 | import org.apache.pluto.PortletWindowID; |
24 | |
25 | /** |
26 | * Wraps around the internal Object IDs. By holding both the string and the |
27 | * integer version of an Object ID this class helps speed up the internal |
28 | * processing. |
29 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
30 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
31 | */ |
32 | public class PortletWindowIDImpl implements PortletWindowID, Serializable { |
33 | |
34 | // Private Member Variables ------------------------------------------------ |
35 | |
36 | private String stringId = null; |
37 | private int intId; |
38 | |
39 | |
40 | // Constructor ------------------------------------------------------------- |
41 | |
42 | /** |
43 | * Private constructor that prevents external instantiation. |
44 | * @param intId the integer ID. |
45 | * @param stringId the string ID. |
46 | */ |
47 | private PortletWindowIDImpl(int intId, String stringId) { |
48 | this.stringId = stringId; |
49 | this.intId = intId; |
50 | } |
51 | |
52 | |
53 | // PortletWindowID Impl ---------------------------------------------------- |
54 | |
55 | public String getStringId() { |
56 | return stringId; |
57 | } |
58 | |
59 | // Internal Methods -------------------------------------------------------- |
60 | |
61 | private void readObject(ObjectInputStream stream) throws IOException { |
62 | intId = stream.readInt(); |
63 | stringId = String.valueOf(intId); |
64 | } |
65 | |
66 | private void writeObject(ObjectOutputStream stream) throws IOException { |
67 | stream.write(intId); |
68 | } |
69 | |
70 | // Common Object Methods --------------------------------------------------- |
71 | |
72 | /** |
73 | public boolean equals(Object object) { |
74 | boolean result = false; |
75 | if (object instanceof PortletWindowIDImpl) { |
76 | result = (intId == ((PortletWindowIDImpl) object).intId); |
77 | } else if (object instanceof String) { |
78 | result = stringId.equals(object); |
79 | } else if (object instanceof Integer) { |
80 | result = (intId == ((Integer) object).intValue()); |
81 | } |
82 | return (result); |
83 | } |
84 | **/ |
85 | |
86 | public int hashCode() { |
87 | return intId; |
88 | } |
89 | |
90 | |
91 | // Additional Methods ------------------------------------------------------ |
92 | |
93 | public int intValue() { |
94 | return intId; |
95 | } |
96 | |
97 | /** |
98 | * Creates a portlet window ID instance from a string. |
99 | * @param stringId the string ID from which the instance is created. |
100 | * @return a portlet window ID instance created from the string ID. |
101 | */ |
102 | static public PortletWindowIDImpl createFromString(String stringId) { |
103 | char[] id = stringId.toCharArray(); |
104 | int _id = 1; |
105 | for (int i = 0; i < id.length; i++) { |
106 | if ((i % 2) == 0) { |
107 | _id *= id[i]; |
108 | } else { |
109 | _id ^= id[i]; |
110 | } |
111 | _id = Math.abs(_id); |
112 | } |
113 | return new PortletWindowIDImpl(_id, stringId); |
114 | } |
115 | |
116 | } |