001: /**************************************************************************/
002: /* B O S S A */
003: /* A simple imperative object-oriented research language */
004: /* (c) Daniel Bonniot 1999 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package bossa.util;
012:
013: import java.util.*;
014:
015: /**
016: Set of useful static methods.
017: */
018: public class Util {
019: /****************************************************************
020: * String Iterations
021: ****************************************************************/
022:
023: /** iterates toString on the collection */
024: public static String map(Collection c) {
025: return map("", "", "", c);
026: }
027:
028: public static String map(String init, String delim, String end,
029: Collection c) {
030: if (c == null || c.size() == 0)
031: return "";
032:
033: String res = init;
034:
035: Iterator i = c.iterator();
036: while (i.hasNext()) {
037: res += String.valueOf(i.next());
038: if (i.hasNext())
039: res += delim;
040: }
041: return res + end;
042: }
043:
044: public static String map(String init, String delim, String end,
045: Collection c, int param) {
046: if (c == null || c.size() == 0)
047: return "";
048:
049: String res = init;
050:
051: Iterator i = c.iterator();
052: while (i.hasNext()) {
053: Object o = i.next();
054: if (o instanceof Printable)
055: res += ((Printable) o).toString(param);
056: else
057: res += o.toString();
058: if (i.hasNext())
059: res += delim;
060: }
061: return res + end;
062: }
063:
064: public static String map(String init, String delim, String end,
065: boolean alwaysInitEnd, Collection c) {
066: if (alwaysInitEnd)
067: return init + map("", delim, "", c) + end;
068:
069: switch (c.size()) {
070: case 0:
071: return "";
072: case 1:
073: return c.iterator().next().toString();
074: default:
075: return map(init, delim, end, c);
076: }
077: }
078:
079: /** iterates on a set of pairs */
080: public static String map(String init, boolean keyFirst,
081: String delim1, String delim2, String end, Set s) {
082: String res = init;
083: Iterator i;
084: i = s.iterator();
085:
086: while (i.hasNext()) {
087: Map.Entry e = (Map.Entry) i.next();
088: res += e.getValue().toString() + delim1
089: + e.getKey().toString() + delim2;
090: }
091: return res + end;
092: }
093:
094: public static String map(String init, String delim, String end,
095: Object[] c) {
096: if (c == null || c.length == 0)
097: return "";
098:
099: String res = init;
100:
101: for (int i = 0; i < c.length - 1; i++)
102: res += String.valueOf(c[i]) + delim;
103: return res + c[c.length - 1] + end;
104: }
105:
106: /****************************************************************
107: * Names
108: ****************************************************************/
109:
110: public static String simpleName(String fullyQualifiedName) {
111: int lastDot = fullyQualifiedName.lastIndexOf('.');
112: return fullyQualifiedName.substring(lastDot + 1);
113: }
114:
115: /****************************************************************
116: * English
117: ****************************************************************/
118:
119: public static String plural(int number, String word) {
120: StringBuffer res = new StringBuffer(number == 0 ? "no" : ""
121: + number);
122: res.append(' ');
123: res.append(word);
124: if (number != 1)
125: res.append("s");
126: return res.toString();
127: }
128:
129: /**
130: returns:
131: " has no bars"
132: " has 1 bar"
133: " has n bars"
134: or, if 0 < number < not
135: " has only 1 bar"
136: " has only n bars"
137: */
138: public static String has(int number, String word, int not) {
139: StringBuffer res = new StringBuffer(" has ");
140: if (0 < number && number < not)
141: res.append("only ");
142: res.append(number == 0 ? "no" : "" + number);
143: res.append(' ');
144: res.append(word);
145: if (number != 1)
146: res.append('s');
147: return res.toString();
148: }
149: }
|