Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003 The Apache Software Foundation
   *
   *  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.
  */
 package org.mockito.cglib.util;
 
 import java.util.*;
 
This class implements a simple String->int mapping for a fixed set of keys.
 
 abstract public class StringSwitcher {
     private static final Type STRING_SWITCHER =
       TypeUtils.parseType("org.mockito.cglib.util.StringSwitcher");
     private static final Signature INT_VALUE =
       TypeUtils.parseSignature("int intValue(String)");
     private static final StringSwitcherKey KEY_FACTORY =
       (StringSwitcherKey)KeyFactory.create(StringSwitcherKey.class);
 
     interface StringSwitcherKey {
         public Object newInstance(String[] stringsint[] intsboolean fixedInput);
     }

    
Helper method to create a StringSwitcher. For finer control over the generated instance, use a new instance of StringSwitcher.Generator instead of this static method.

Parameters:
strings the array of String keys; must be the same length as the value array
ints the array of integer results; must be the same length as the key array
fixedInput if false, an unknown key will be returned from intValue(java.lang.String) as -1; if true, the result will be undefined, and the resulting code will be faster
 
     public static StringSwitcher create(String[] stringsint[] intsboolean fixedInput) {
         Generator gen = new Generator();
         gen.setStrings(strings);
         gen.setInts(ints);
         gen.setFixedInput(fixedInput);
         return gen.create();
     }
 
     protected StringSwitcher() {
     }

    
Return the integer associated with the given key.

Parameters:
s the key
Returns:
the associated integer value, or -1 if the key is unknown (unless fixedInput was specified when this StringSwitcher was created, in which case the return value for an unknown key is undefined)
 
     abstract public int intValue(String s);
 
     public static class Generator extends AbstractClassGenerator {
         private static final Source SOURCE = new Source(StringSwitcher.class.getName());
 
         private String[] strings;
         private int[] ints;
         private boolean fixedInput;
         
         public Generator() {
             super();
         }

        
Set the array of recognized Strings.

Parameters:
strings the array of String keys; must be the same length as the value array
See also:
setInts(int[])
 
         public void setStrings(String[] strings) {
             this. = strings;
         }

        
Set the array of integer results.

Parameters:
ints the array of integer results; must be the same length as the key array
See also:
setStrings(java.lang.String[])
 
         public void setInts(int[] ints) {
             this. = ints;
         }

        
Configure how unknown String keys will be handled.

Parameters:
fixedInput if false, an unknown key will be returned from StringSwitcher.intValue(java.lang.String) as -1; if true, the result will be undefined, and the resulting code will be faster
        public void setFixedInput(boolean fixedInput) {
            this. = fixedInput;
        }
        protected ClassLoader getDefaultClassLoader() {
            return getClass().getClassLoader();
        }

        
Generate the StringSwitcher.
        public StringSwitcher create() {
            setNamePrefix(StringSwitcher.class.getName());
            Object key = .newInstance();
            return (StringSwitcher)super.create(key);
        }
        public void generateClass(ClassVisitor vthrows Exception {
            ClassEmitter ce = new ClassEmitter(v);
            ce.begin_class(.,
                           .,
                           getClassName(),
                           ,
                           null,
                           .);
            EmitUtils.null_constructor(ce);
            final CodeEmitter e = ce.begin_method(.null);
            e.load_arg(0);
            final List stringList = Arrays.asList();
            int style =  ? . : .;
            EmitUtils.string_switch(estylenew ObjectSwitchCallback() {
                public void processCase(Object keyLabel end) {
                    e.push([stringList.indexOf(key)]);
                    e.return_value();
                }
                public void processDefault() {
                    e.push(-1);
                    e.return_value();
                }
            });
            e.end_method();
            ce.end_class();
        }
        protected Object firstInstance(Class type) {
            return (StringSwitcher)ReflectUtils.newInstance(type);
        }
        protected Object nextInstance(Object instance) {
            return instance;
        }
    }
New to GrepCode? Check out our FAQ X