Home | History | Annotate | Download | only in misc
      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  */
     28 package org.antlr.misc;
     29 
     30 public class Utils {
     31 	public static final int INTEGER_POOL_MAX_VALUE = 1000;
     32 	static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE+1];
     33 
     34 	/** Integer objects are immutable so share all Integers with the
     35 	 *  same value up to some max size.  Use an array as a perfect hash.
     36 	 *  Return shared object for 0..INTEGER_POOL_MAX_VALUE or a new
     37 	 *  Integer object with x in it.
     38 	 */
     39 	public static Integer integer(int x) {
     40 		if ( x<0 || x>INTEGER_POOL_MAX_VALUE ) {
     41 			return x;
     42 		}
     43 		if ( ints[x]==null ) {
     44 			ints[x] = x;
     45 		}
     46 		return ints[x];
     47 	}
     48 
     49 	/** Given a source string, src,
     50 		a string to replace, replacee,
     51 		and a string to replace with, replacer,
     52 		return a new string w/ the replacing done.
     53 		You can use replacer==null to remove replacee from the string.
     54 
     55 		This should be faster than Java's String.replaceAll as that one
     56 		uses regex (I only want to play with strings anyway).
     57 	*/
     58 	public static String replace(String src, String replacee, String replacer) {
     59 		StringBuilder result = new StringBuilder(src.length() + 50);
     60 		int startIndex = 0;
     61 		int endIndex = src.indexOf(replacee);
     62 		while(endIndex != -1) {
     63 			result.append(src.substring(startIndex,endIndex));
     64 			if ( replacer!=null ) {
     65 				result.append(replacer);
     66 			}
     67 			startIndex = endIndex + replacee.length();
     68 			endIndex = src.indexOf(replacee,startIndex);
     69 		}
     70 		result.append(src.substring(startIndex,src.length()));
     71 		return result.toString();
     72 	}
     73 
     74 //	/** mimic struct; like a non-iterable map. */
     75 //	public static class Struct {
     76 //		public Map<String,Object> fields = new HashMap<String,Object>();
     77 //
     78 //		@Override
     79 //		public String toString() { return fields.toString(); }
     80 //	}
     81 //
     82 //	public static Struct struct(String propNames, Object... values) {
     83 //		String[] props = propNames.split(",");
     84 //		int i=0;
     85 //		Struct s = new Struct();
     86 //		for (String p : props) s.fields.put(p, values[i++]);
     87 //		return s;
     88 //	}
     89 }
     90