1/*
2 * [The "BSD license"]
3 *  Copyright (c) 2010 Terence Parr
4 *  All rights reserved.
5 *
6 *  Redistribution and use in source and binary forms, with or without
7 *  modification, are permitted provided that the following conditions
8 *  are met:
9 *  1. Redistributions of source code must retain the above copyright
10 *      notice, this list of conditions and the following disclaimer.
11 *  2. Redistributions in binary form must reproduce the above copyright
12 *      notice, this list of conditions and the following disclaimer in the
13 *      documentation and/or other materials provided with the distribution.
14 *  3. The name of the author may not be used to endorse or promote products
15 *      derived from this software without specific prior written permission.
16 *
17 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.misc;
29
30import java.util.HashMap;
31import java.util.Map;
32
33public class Utils {
34	public static final int INTEGER_POOL_MAX_VALUE = 1000;
35	static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1];
36
37	/** Integer objects are immutable so share all Integers with the
38	 *  same value up to some max size.  Use an array as a perfect hash.
39	 *  Return shared object for 0..INTEGER_POOL_MAX_VALUE or a new
40	 *  Integer object with x in it.
41	 */
42	public static Integer integer(int x) {
43		if ( x<0 || x>INTEGER_POOL_MAX_VALUE ) {
44			return new Integer(x);
45		}
46		if ( ints[x]==null ) {
47			ints[x] = new Integer(x);
48		}
49		return ints[x];
50	}
51
52	/** Given a source string, src,
53		a string to replace, replacee,
54		and a string to replace with, replacer,
55		return a new string w/ the replacing done.
56		You can use replacer==null to remove replacee from the string.
57
58		This should be faster than Java's String.replaceAll as that one
59		uses regex (I only want to play with strings anyway).
60	*/
61	public static String replace(String src, String replacee, String replacer) {
62		StringBuffer result = new StringBuffer(src.length() + 50);
63		int startIndex = 0;
64		int endIndex = src.indexOf(replacee);
65		while(endIndex != -1) {
66			result.append(src.substring(startIndex,endIndex));
67			if ( replacer!=null ) {
68				result.append(replacer);
69			}
70			startIndex = endIndex + replacee.length();
71			endIndex = src.indexOf(replacee,startIndex);
72		}
73		result.append(src.substring(startIndex,src.length()));
74		return result.toString();
75	}
76
77//	/** mimic struct; like a non-iterable map. */
78//	public static class Struct {
79//		public Map<String,Object> fields = new HashMap<String,Object>();
80//
81//		@Override
82//		public String toString() { return fields.toString(); }
83//	}
84//
85//	public static Struct struct(String propNames, Object... values) {
86//		String[] props = propNames.split(",");
87//		int i=0;
88//		Struct s = new Struct();
89//		for (String p : props) s.fields.put(p, values[i++]);
90//		return s;
91//	}
92}
93