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

org.snapscript.core.property.PropertyExtractor Maven / Gradle / Ivy

package org.snapscript.core.property;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.snapscript.core.EntityCache;
import org.snapscript.core.type.Type;
import org.snapscript.core.type.TypeExtractor;

public class PropertyExtractor {

   private final EntityCache> cache;
   private final TypeExtractor extractor;
   
   public PropertyExtractor(TypeExtractor extractor) {
      this.cache = new EntityCache>();
      this.extractor = extractor;
   }
   
   public Set findProperties(Type type) {
      Set properties = cache.fetch(type);
      
      if(properties == null) {
         properties = findHierarchy(type);
         cache.cache(type, properties);
      }
      return properties;
   }
   
   private Set findHierarchy(Type type) {
      Set types = extractor.getTypes(type);
      
      if(!types.isEmpty()) {
         Set done = new LinkedHashSet();
         Set result = new LinkedHashSet();
         
         for(Type base : types) {
            Set map = findProperties(base, done);
            result.addAll(map);
         }
         return result;
      }
      return Collections.emptySet();
   }
   
   private Set findProperties(Type type, Set done) {
      List properties = type.getProperties();
      
      if(!properties.isEmpty()) {
         Set result = new LinkedHashSet();
         
         for(Property property : properties) {
            String name = property.getName();
            
            if(done.add(name)) {
               result.add(property);
            }
         }
         return result;
      }
      return Collections.emptySet();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy