Package com.skyline.energy.definition

Source Code of com.skyline.energy.definition.CacheDefinitionCollection

package com.skyline.energy.definition;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.skyline.energy.annotation.Cache;
import com.skyline.energy.annotation.CacheDelete;
import com.skyline.energy.annotation.CacheUpdate;
import com.skyline.energy.annotation.VerUpdate;
import com.skyline.energy.exception.DaoGenerateException;

public class CacheDefinitionCollection {
  private static final Log LOGGER = LogFactory.getLog(CacheDefinitionCollection.class);
  private CacheDefinition cacheDefinition;
  private List<CacheDeleteDefinition> cacheDeleteDefinitions;
  private List<VersionUpdateDefinition> versionUpdateDefinitions;

  public CacheDefinitionCollection(Method method) throws DaoGenerateException {

    Cache cache = method.getAnnotation(Cache.class);
    if (cache != null) {
      cacheDefinition = new CacheDefinition(cache, method);
    }

    CacheDelete cacheDelete = method.getAnnotation(CacheDelete.class);
    if (cacheDelete != null) {
      CacheDeleteDefinition definition = new CacheDeleteDefinition(cacheDelete, method);
      cacheDeleteDefinitions = lazyInit(cacheDeleteDefinitions);
      cacheDeleteDefinitions.add(definition);
    }

    VerUpdate versionUpdate = method.getAnnotation(VerUpdate.class);
    if (versionUpdate != null) {
      VersionUpdateDefinition definition = new VersionUpdateDefinition(versionUpdate, method);
      versionUpdateDefinitions = lazyInit(versionUpdateDefinitions);
      versionUpdateDefinitions.add(definition);
    }

    CacheUpdate cacheUpdate = method.getAnnotation(CacheUpdate.class);
    if (cacheUpdate != null) {
      parseCacheUpdateDeiniftion(cacheUpdate, method);
    }

    if (cacheDefinition != null && (cacheDeleteDefinitions != null || versionUpdateDefinitions != null)) {
      LOGGER.info("@Cache can't be used with other Cache Annotation, and it will be not effective");
      cacheDefinition = null;
    }
  }

  public boolean needCacheOpration() {
    return (cacheDefinition != null || cacheDeleteDefinitions != null || versionUpdateDefinitions != null);
  }

  public CacheDefinition getCacheDefinition() {
    return cacheDefinition;
  }

  public List<CacheDeleteDefinition> getCacheDeleteDefinitions() {
    return cacheDeleteDefinitions;
  }

  public List<VersionUpdateDefinition> getVersionUpdateDefinitions() {
    return versionUpdateDefinitions;
  }

  private <T> List<T> lazyInit(List<T> list) {
    if (list == null) {
      return new ArrayList<T>(1);
    } else {
      return list;
    }
  }

  private void parseCacheUpdateDeiniftion(CacheUpdate cacheUpdate, Method method) throws DaoGenerateException {
    CacheDelete[] deletes = cacheUpdate.delete();
    VerUpdate[] updates = cacheUpdate.update();

    for (CacheDelete delete : deletes) {
      CacheDeleteDefinition definition = new CacheDeleteDefinition(delete, method);
      cacheDeleteDefinitions = lazyInit(cacheDeleteDefinitions);
      cacheDeleteDefinitions.add(definition);
    }

    for (VerUpdate update : updates) {
      VersionUpdateDefinition definition = new VersionUpdateDefinition(update, method);
      versionUpdateDefinitions = lazyInit(versionUpdateDefinitions);
      versionUpdateDefinitions.add(definition);
    }
  }

}
TOP

Related Classes of com.skyline.energy.definition.CacheDefinitionCollection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.