Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.commons.marshall;
  
  
Class providing hints about marshallable types, such as whether a particular type is marshallable or not, or an accurate approach to the serialized size of a particular type.

Author(s):
Galder ZamarreƱo
Since:
5.1
 
 public final class MarshallableTypeHints {
 
    private static final Log log = LogFactory.getLog(MarshallableTypeHints.class);
    private static final boolean trace = .isTraceEnabled();

   
Cache of classes that are considered to be marshallable alongside their buffer size predictor. Since checking whether a type is marshallable requires attempting to marshalling them, a cache for the types that are known to be marshallable or not is advantageous.
 
    private final ConcurrentMap<Class<?>, MarshallingTypetypeHints =
          CollectionFactory.makeConcurrentMap();

   
Get the serialized form size predictor for a particular type.

Parameters:
type Marshallable type for which serialized form size will be predicted
Returns:
an instance of BufferSizePredictor
 
    public BufferSizePredictor getBufferSizePredictor(Class<?> type) {
       MarshallingType marshallingType = .get(type);
       if (marshallingType == null) {
          // Initialise with isMarshallable to null, meaning it's unknown
          marshallingType = new MarshallingType(nullnew AdaptiveBufferSizePredictor());
          MarshallingType prev = .putIfAbsent(typemarshallingType);
          if (prev != null) {
             marshallingType = prev;
          } else {
             if () {
                .tracef("Cache a buffer size predictor for '%s' assuming " +
                      "its serializability is unknown"type.getName());
             }
          }
       }
       return marshallingType.sizePredictor;
    }

   
Returns whether the hint on whether a particular type is marshallable or not is available. This method can be used to avoid attempting to marshall a type, if the hints for the type have already been calculated.

Parameters:
type Marshallable type to check whether an attempt to mark it as marshallable has been made.
Returns:
true if the type has been marked as marshallable at all, false if no attempt has been made to mark the type as marshallable.
 
    public boolean isKnownMarshallable(Class<?> type) {
       MarshallingType marshallingType = .get(type);
       return marshallingType != null && marshallingType.isMarshallable != null;
    }

   
Returns whether a type can be serialized. In order for a type to be considered marshallable, the type must have been marked as marshallable using the markMarshallable(java.lang.Class,boolean) method earlier, passing true as parameter. If a type has not yet been marked as marshallable, this method will return false.
 
    public boolean isMarshallable(Class<?> type) {
       MarshallingType marshallingType = .get(type);
       if (marshallingType != null) {
          Boolean marshallable = marshallingType.isMarshallable;
          return marshallable != null ? marshallable.booleanValue() : false;
       }
 
       return false;
    }

   
Marks a particular type as being marshallable or not being not marshallable.

Parameters:
type Class to mark as serializable or non-serializable
isMarshallable Whether the type can be marshalled or not.
 
    public void markMarshallable(Class<?> typeboolean isMarshallable) {
       MarshallingType marshallType = .get(type);
       if (marshallableUpdateRequired(isMarshallablemarshallType)) {
          boolean replaced = .replace(typemarshallTypenew MarshallingType(
                Boolean.valueOf(isMarshallable), marshallType.sizePredictor));
          if (replaced && ) {
             .tracef("Replacing '%s' type to be marshallable=%b",
                  type.getName(), isMarshallable);
         }
      } else if (marshallType == null) {
         if () {
            .tracef("Cache '%s' type to be marshallable=%b",
                  type.getName(), isMarshallable);
         }
         .put(typenew MarshallingType(
               Boolean.valueOf(isMarshallable), new AdaptiveBufferSizePredictor()));
      }
   }

   
Clear the cached marshallable type hints.
   public void clear() {
      .clear();
   }
   private boolean marshallableUpdateRequired(boolean isMarshallable,
         MarshallingType marshallType) {
      return marshallType != null &&
            (marshallType.isMarshallable == null ||
                   marshallType.isMarshallable.booleanValue() != isMarshallable);
   }
   private static class MarshallingType {
      final Boolean isMarshallable;
      private MarshallingType(Boolean marshallableBufferSizePredictor sizePredictor) {
          = marshallable;
         this. = sizePredictor;
      }
      @Override
      public boolean equals(Object o) {
         if (this == oreturn true;
         if (o == null || getClass() != o.getClass()) return false;
         MarshallingType that = (MarshallingTypeo;
         if ( != null ? !.equals(that.isMarshallable) : that.isMarshallable != null)
            return false;
         if ( != null ? !.equals(that.sizePredictor) : that.sizePredictor != null)
            return false;
         return true;
      }
      @Override
      public int hashCode() {
         int result =  != null ? .hashCode() : 0;
         result = 31 * result + ( != null ? .hashCode() : 0);
         return result;
      }
   }
New to GrepCode? Check out our FAQ X