Mega Code Archive

 
Categories / Java / GWT
 

Generate a faster string comparison than String equals() for web mode code

/*  * Copyright 2009 Sysmap Solutions Software e Consultoria Ltda.  *   * Licensed under the Apache License, Version 2.0 (the "License"); you may not  * use this file except in compliance with the License. You may obtain a copy of  * the License at  *   * http://www.apache.org/licenses/LICENSE-2.0  *   * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the  * License for the specific language governing permissions and limitations under  * the License.  */ /**  * @author Gesse S. F. Dafe  * @author Thiago da Rosa de Bustamante -  */    import com.google.gwt.core.client.GWT; public class Util{   /**    * This method generates a faster string comparison than {@code String.equals()}    * for web mode code.    *     * The GWT generated {@code String.equals()} method makes a check in its parameter's type in runtime.     * That method makes that assertion on compilation time (once its parameters are both     * Strings).    *     * @param a    * @param b    * @return    */   public static boolean equals(String a, String b)   {     if (GWT.isScript())     {       if (a==null) return (b==null);       return (b!= null && a==b);     }     else     {       if (a==null) return (b==null);       return (b!= null && a.equals(b));     }   }    }