001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.catalina.tribes.util;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021:
022: import org.apache.catalina.tribes.ChannelMessage;
023: import org.apache.catalina.tribes.Member;
024: import org.apache.catalina.tribes.UniqueId;
025: import org.apache.catalina.tribes.group.AbsoluteOrder;
026: import org.apache.catalina.tribes.membership.MemberImpl;
027: import org.apache.catalina.tribes.membership.Membership;
028: import java.io.UnsupportedEncodingException;
029: import org.apache.juli.logging.Log;
030: import org.apache.juli.logging.LogFactory;
031: import java.util.StringTokenizer;
032:
033: /**
034: * @author Filip Hanik
035: * @version 1.0
036: */
037: public class Arrays {
038: protected static Log log = LogFactory.getLog(Arrays.class);
039:
040: public static boolean contains(byte[] source, int srcoffset,
041: byte[] key, int keyoffset, int length) {
042: if (srcoffset < 0 || srcoffset >= source.length)
043: throw new ArrayIndexOutOfBoundsException(
044: "srcoffset is out of bounds.");
045: if (keyoffset < 0 || keyoffset >= key.length)
046: throw new ArrayIndexOutOfBoundsException(
047: "keyoffset is out of bounds.");
048: if (length > (key.length - keyoffset))
049: throw new ArrayIndexOutOfBoundsException(
050: "not enough data elements in the key, length is out of bounds.");
051: //we don't have enough data to validate it
052: if (length > (source.length - srcoffset))
053: return false;
054: boolean match = true;
055: int pos = keyoffset;
056: for (int i = srcoffset; match && i < length; i++) {
057: match = (source[i] == key[pos++]);
058: }
059: return match;
060: }
061:
062: public static String toString(byte[] data) {
063: return toString(data, 0, data != null ? data.length : 0);
064: }
065:
066: public static String toString(byte[] data, int offset, int length) {
067: StringBuffer buf = new StringBuffer("{");
068: if (data != null && length > 0) {
069: buf.append(data[offset++]);
070: for (int i = offset; i < length; i++) {
071: buf.append(", ").append(data[i]);
072: }
073: }
074: buf.append("}");
075: return buf.toString();
076: }
077:
078: public static String toString(Object[] data) {
079: return toString(data, 0, data != null ? data.length : 0);
080: }
081:
082: public static String toString(Object[] data, int offset, int length) {
083: StringBuffer buf = new StringBuffer("{");
084: if (data != null && length > 0) {
085: buf.append(data[offset++]);
086: for (int i = offset; i < length; i++) {
087: buf.append(", ").append(data[i]);
088: }
089: }
090: buf.append("}");
091: return buf.toString();
092: }
093:
094: public static String toNameString(Member[] data) {
095: return toNameString(data, 0, data != null ? data.length : 0);
096: }
097:
098: public static String toNameString(Member[] data, int offset,
099: int length) {
100: StringBuffer buf = new StringBuffer("{");
101: if (data != null && length > 0) {
102: buf.append(data[offset++].getName());
103: for (int i = offset; i < length; i++) {
104: buf.append(", ").append(data[i].getName());
105: }
106: }
107: buf.append("}");
108: return buf.toString();
109: }
110:
111: public static int add(int[] data) {
112: int result = 0;
113: for (int i = 0; i < data.length; i++)
114: result += data[i];
115: return result;
116: }
117:
118: public static UniqueId getUniqudId(ChannelMessage msg) {
119: return new UniqueId(msg.getUniqueId());
120: }
121:
122: public static UniqueId getUniqudId(byte[] data) {
123: return new UniqueId(data);
124: }
125:
126: public static boolean equals(byte[] o1, byte[] o2) {
127: return java.util.Arrays.equals(o1, o2);
128: }
129:
130: public static boolean equals(Object[] o1, Object[] o2) {
131: boolean result = o1.length == o2.length;
132: if (result)
133: for (int i = 0; i < o1.length && result; i++)
134: result = o1[i].equals(o2[i]);
135: return result;
136: }
137:
138: public static boolean sameMembers(Member[] m1, Member[] m2) {
139: AbsoluteOrder.absoluteOrder(m1);
140: AbsoluteOrder.absoluteOrder(m2);
141: return equals(m1, m2);
142: }
143:
144: public static Member[] merge(Member[] m1, Member[] m2) {
145: AbsoluteOrder.absoluteOrder(m1);
146: AbsoluteOrder.absoluteOrder(m2);
147: ArrayList list = new ArrayList(java.util.Arrays.asList(m1));
148: for (int i = 0; i < m2.length; i++)
149: if (!list.contains(m2[i]))
150: list.add(m2[i]);
151: Member[] result = new Member[list.size()];
152: list.toArray(result);
153: AbsoluteOrder.absoluteOrder(result);
154: return result;
155: }
156:
157: public static void fill(Membership mbrship, Member[] m) {
158: for (int i = 0; i < m.length; i++)
159: mbrship.addMember((MemberImpl) m[i]);
160: }
161:
162: public static Member[] diff(Membership complete, Membership local,
163: MemberImpl ignore) {
164: ArrayList result = new ArrayList();
165: MemberImpl[] comp = complete.getMembers();
166: for (int i = 0; i < comp.length; i++) {
167: if (ignore != null && ignore.equals(comp[i]))
168: continue;
169: if (local.getMember(comp[i]) == null)
170: result.add(comp[i]);
171: }
172: return (MemberImpl[]) result.toArray(new MemberImpl[result
173: .size()]);
174: }
175:
176: public static Member[] remove(Member[] all, Member remove) {
177: return extract(all, new Member[] { remove });
178: }
179:
180: public static Member[] extract(Member[] all, Member[] remove) {
181: List alist = java.util.Arrays.asList(all);
182: ArrayList list = new ArrayList(alist);
183: for (int i = 0; i < remove.length; i++)
184: list.remove(remove[i]);
185: return (Member[]) list.toArray(new Member[list.size()]);
186: }
187:
188: public static int indexOf(Member member, Member[] members) {
189: int result = -1;
190: for (int i = 0; (result == -1) && (i < members.length); i++)
191: if (member.equals(members[i]))
192: result = i;
193: return result;
194: }
195:
196: public static int nextIndex(Member member, Member[] members) {
197: int idx = indexOf(member, members) + 1;
198: if (idx >= members.length)
199: idx = ((members.length > 0) ? 0 : -1);
200:
201: //System.out.println("Next index:"+idx);
202: //System.out.println("Member:"+member.getName());
203: //System.out.println("Members:"+toNameString(members));
204: return idx;
205: }
206:
207: public static int hashCode(byte a[]) {
208: if (a == null)
209: return 0;
210:
211: int result = 1;
212: for (int i = 0; i < a.length; i++) {
213: byte element = a[i];
214: result = 31 * result + element;
215: }
216: return result;
217: }
218:
219: public static byte[] fromString(String value) {
220: if (value == null)
221: return null;
222: if (!value.startsWith("{"))
223: throw new RuntimeException(
224: "byte arrays must be represented as {1,3,4,5,6}");
225: StringTokenizer t = new StringTokenizer(value, "{,}", false);
226: byte[] result = new byte[t.countTokens()];
227: for (int i = 0; i < result.length; i++)
228: result[i] = Byte.parseByte(t.nextToken());
229: return result;
230: }
231:
232: public static byte[] convert(String s) {
233: try {
234: return s.getBytes("ISO-8859-1");
235: } catch (UnsupportedEncodingException ux) {
236: log
237: .error("Unable to convert ["
238: + s
239: + "] into a byte[] using ISO-8859-1 encoding, falling back to default encoding.");
240: return s.getBytes();
241: }
242: }
243:
244: }
|