001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.util;
008:
009: import java.util.*;
010: import com.ibm.icu.text.UnicodeSet;
011: import com.ibm.icu.text.UnicodeSetIterator;
012:
013: public abstract class Visitor {
014:
015: public void doAt(Object item) {
016: if (item instanceof Collection) {
017: doAt((Collection) item);
018: } else if (item instanceof Map) {
019: doAt((Map) item);
020: } else if (item instanceof Object[]) {
021: doAt((Object[]) item);
022: } else if (item instanceof UnicodeSet) {
023: doAt((UnicodeSet) item);
024: } else {
025: doSimpleAt(item);
026: }
027: }
028:
029: public int count(Object item) {
030: if (item instanceof Collection) {
031: return ((Collection) item).size();
032: } else if (item instanceof Map) {
033: return ((Map) item).size();
034: } else if (item instanceof Object[]) {
035: return ((Object[]) item).length;
036: } else if (item instanceof UnicodeSet) {
037: return ((UnicodeSet) item).size();
038: } else {
039: return 1;
040: }
041: }
042:
043: // the default implementation boxing
044:
045: public void doAt(int o) {
046: doSimpleAt(new Integer(o));
047: }
048:
049: public void doAt(double o) {
050: doSimpleAt(new Double(o));
051: }
052:
053: public void doAt(char o) {
054: doSimpleAt(new Character(o));
055: }
056:
057: // for subclassing
058:
059: protected void doAt(Collection c) {
060: if (c.size() == 0)
061: doBefore(c, null);
062: Iterator it = c.iterator();
063: boolean first = true;
064: Object last = null;
065: while (it.hasNext()) {
066: Object item = it.next();
067: if (first) {
068: doBefore(c, item);
069: first = false;
070: } else {
071: doBetween(c, last, item);
072: }
073: doAt(last = item);
074: }
075: doAfter(c, last);
076: }
077:
078: protected void doAt(Map c) {
079: doAt(c.entrySet());
080: }
081:
082: protected void doAt(UnicodeSet c) {
083: if (c.size() == 0)
084: doBefore(c, null);
085: UnicodeSetIterator it = new UnicodeSetIterator(c);
086: boolean first = true;
087: Object last = null;
088: Object item;
089: CodePointRange cpr0 = new CodePointRange();
090: CodePointRange cpr1 = new CodePointRange();
091: CodePointRange cpr;
092:
093: while (it.nextRange()) {
094: if (it.codepoint == UnicodeSetIterator.IS_STRING) {
095: item = it.string;
096: } else {
097: cpr = last == cpr0 ? cpr1 : cpr0; // make sure we don't override last
098: cpr.codepoint = it.codepoint;
099: cpr.codepointEnd = it.codepointEnd;
100: item = cpr;
101: }
102: if (!first) {
103: doBefore(c, item);
104: first = true;
105: } else {
106: doBetween(c, last, item);
107: }
108: doAt(last = item);
109: }
110: doAfter(c, last);
111: }
112:
113: protected void doAt(Object[] c) {
114: doBefore(c, c.length == 0 ? null : c[0]);
115: Object last = null;
116: for (int i = 0; i < c.length; ++i) {
117: if (i != 0)
118: doBetween(c, last, c[i]);
119: doAt(last = c[i]);
120: }
121: doAfter(c, last);
122: }
123:
124: public static class CodePointRange {
125: public int codepoint, codepointEnd;
126: }
127:
128: // ===== MUST BE OVERRIDEN =====
129:
130: abstract protected void doBefore(Object container, Object item);
131:
132: abstract protected void doBetween(Object container,
133: Object lastItem, Object nextItem);
134:
135: abstract protected void doAfter(Object container, Object item);
136:
137: abstract protected void doSimpleAt(Object o);
138:
139: }
|