Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * 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 com.google.common.primitives;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 import java.util.Map;
 import java.util.Set;

Contains static utility methods pertaining to primitive types and their corresponding wrapper types.

Author(s):
Kevin Bourrillion
Since:
1.0
 
 public final class Primitives {
   private Primitives() {}

  
A map from primitive types to their corresponding wrapper types.
 
   private static final Map<Class<?>, Class<?>> PRIMITIVE_TO_WRAPPER_TYPE;

  
A map from wrapper types to their corresponding primitive types.
 
   private static final Map<Class<?>, Class<?>> WRAPPER_TO_PRIMITIVE_TYPE;
 
   // Sad that we can't use a BiMap. :(
   
   static {
     Map<Class<?>, Class<?>> primToWrap = new HashMap<Class<?>, Class<?>>(16);
     Map<Class<?>, Class<?>> wrapToPrim = new HashMap<Class<?>, Class<?>>(16);
 
     add(primToWrapwrapToPrimboolean.classBoolean.class);
     add(primToWrapwrapToPrimbyte.classByte.class);
     add(primToWrapwrapToPrimchar.classCharacter.class);
     add(primToWrapwrapToPrimdouble.classDouble.class);
     add(primToWrapwrapToPrimfloat.classFloat.class);
     add(primToWrapwrapToPrimint.classInteger.class);
     add(primToWrapwrapToPrimlong.classLong.class);
     add(primToWrapwrapToPrimshort.classShort.class);
     add(primToWrapwrapToPrimvoid.classVoid.class);
 
      = Collections.unmodifiableMap(primToWrap);
      = Collections.unmodifiableMap(wrapToPrim);
   }
 
   private static void add(Map<Class<?>, Class<?>> forward,
       Map<Class<?>, Class<?>> backwardClass<?> keyClass<?> value) {
     forward.put(keyvalue);
     backward.put(valuekey);
   }

  
Returns an immutable set of all nine primitive types (including void). Note that a simpler way to test whether a Class instance is a member of this set is to call java.lang.Class.isPrimitive().

Since:
3.0
 
   public static Set<Class<?>> allPrimitiveTypes() {
     return .keySet();
   }

  
Returns an immutable set of all nine primitive-wrapper types (including java.lang.Void).

Since:
3.0
 
   public static Set<Class<?>> allWrapperTypes() {
     return .keySet();
   }

  
Returns true if type is one of the nine primitive-wrapper types, such as java.lang.Integer.

 
   public static boolean isWrapperType(Class<?> type) {
   }

  
Returns the corresponding wrapper type of type if it is a primitive type; otherwise returns type itself. Idempotent.
     wrap(int.class) == Integer.class
     wrap(Integer.class) == Integer.class
     wrap(String.class) == String.class
 
  public static <T> Class<T> wrap(Class<T> type) {
    checkNotNull(type);
    // cast is safe: long.class and Long.class are both of type Class<Long>
    @SuppressWarnings("unchecked")
    Class<T> wrapped = (Class<T>) .get(type);
    return (wrapped == null) ? type : wrapped;
  }

  
Returns the corresponding primitive type of type if it is a wrapper type; otherwise returns type itself. Idempotent.
     unwrap(Integer.class) == int.class
     unwrap(int.class) == int.class
     unwrap(String.class) == String.class
 
  public static <T> Class<T> unwrap(Class<T> type) {
    checkNotNull(type);
    // cast is safe: long.class and Long.class are both of type Class<Long>
    @SuppressWarnings("unchecked")
    Class<T> unwrapped = (Class<T>) .get(type);
    return (unwrapped == null) ? type : unwrapped;
  }
New to GrepCode? Check out our FAQ X