StandardStateFeature.java :  » App » magicdroid » org » magicdroid » model » impl » Android Open Source

Android Open Source » App » magicdroid 
magicdroid » org » magicdroid » model » impl » StandardStateFeature.java
package org.magicdroid.model.impl;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.magicdroid.commons.Collect;
import org.magicdroid.commons.MagicObject.Concern;
import org.magicdroid.commons.MagicObject.Interface;
import org.magicdroid.commons.MagicObject.Invocation;
import org.magicdroid.commons.Refactor;
import org.magicdroid.features.EntityFeature;

public interface StandardStateFeature {
  
  void internalSet(String name, Object value);
  Object internalGet(String name);
  
  class StateConcern implements Concern, StandardStateFeature {
  
//    private @This StandardStateFeature composite;
    private @Interface Class<? extends StandardStateFeature> type;
  
    private final Map<String, Object> state = new HashMap<String, Object>();
    
    @Override
    public Object invoke(Invocation context) throws Throwable {
      Method method = context.getMethod();
      if (method.getName().equals("toString"))
        return this.toString(this.type);
      
      if (method.getName().equals("equals"))
        return this.equals(context.getParams()[0]);
      
      if (method.getName().equals("hashCode"))
        return this.hashCode();
    
      if (method.getName().equals("internalSet")) {
          this.internalSet((String) context.getParams()[0], context.getParams()[1]);
          return null;
      }
      if (method.getName().equals("internalGet")) {
        return this.internalGet((String) context.getParams()[0]);
      }
//      if (Property.class.isAssignableFrom(method.getReturnType()))
//        return this.properties.get(method.getName());
//    
      if (method.getName().startsWith("set")) {
        this.internalSet(Refactor.extractPropName(method.getName()), context.getParams()[0]);
        return null;
      }
      
      if (method.getName().startsWith("get"))
        return this.state.get(Refactor.extractPropName(method.getName()));
      
      return context.proceed();
    }
    
    @Override
    public void internalSet(String name, Object value) {
      this.state.put(name, value);
//      return this.composite;
    }
    
    @Override
    public Object internalGet(String name) {
      return this.state.get(name);
    }
    
    public String toString(Class type) {
      return type.getSimpleName() + ":(" +
        Collect.toString(this.state.keySet(), ", ", new Collect.Renderer<String>() {
          @Override
          public String format(String input) {
            return input + "=" + state.get(input);
          }
        }) + ")"
        ;
    }
    
    public int hashCode() {
      int result = 0;
      for (Object o : this.state.values())
        if (o != null) result ^= o.hashCode(); 
      return result;
    }
    
    @Override
    public boolean equals(Object obj) {
      if (! (obj instanceof EntityFeature<?>)) return false;
      Map<String, Object> other = ((EntityFeature<?>) obj).metaAsMap();
      return this.state.equals(other);
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.