All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.snapscript.tree.collection.CollectionConverter Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.snapscript.tree.collection;

import java.util.List;
import java.util.Map;

import org.snapscript.core.array.ArrayBuilder;
import org.snapscript.core.error.InternalArgumentException;
import org.snapscript.core.type.Type;

public class CollectionConverter {

   private final ArrayBuilder builder;
   
   public CollectionConverter() {
      this.builder = new ArrayBuilder();
   }
   
   public boolean accept(Object value) throws Exception {
      if(value != null) {
         Class type = value.getClass();
         
         if(type.isArray()) {
            return true;
         }
         if(List.class.isAssignableFrom(type)) {
            return true;
         }
         if(Map.class.isAssignableFrom(type)) {
            return true;
         }
      }
      return false;
   }
   
   public Object convert(Object value) throws Exception {
      if(value != null) {
         Class type = value.getClass();
         
         if(type.isArray()) {
            return builder.convert(value);
         }
         if(List.class.isAssignableFrom(type)) {
            return value;
         }
         if(Map.class.isAssignableFrom(type)) {
            return value;
         }
         if(Type.class.isAssignableFrom(type)) {
            throw new InternalArgumentException("Illegal index of type " + value);            
         }
         throw new InternalArgumentException("Illegal index of " + type);
      }
      return null;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy