java는list를 사용하여 데이터베이스의like기능을 실현한다

list를 사용하여 데이터베이스와 유사한 like,between, 정확한 검색 방법 실현

public final class CommonLocalQuery<T extends Map<String, Object>> {

 // list, : , :
 public List<T> queryData(final List<T> mAllList, final HashHashMap<POLICY, String, String> map) {

  if(mAllList == null | map == null) {
   return new ArrayList<T>();
  }

  if(mAllList.isEmpty()) {
   return mAllList;
  }

  /* */
  Set<POLICY> key1 = map.getFirstKeys();
  for (POLICY policy : key1) {
   ConcurrentHashMap<String, String> tempMap = map.get(policy);
   Set<String> key2 = tempMap.keySet();
   for (String key : key2) {
    if(key.startsWith("-") || tempMap.get(key) == null ||
      (tempMap.get(key) != null && tempMap.get(key).equals(""))) {
     tempMap.remove(key);
    }
   }
  }
//
  Filter filterNormal, filterBetween, filterFuzzy;
  filterNormal = new FilterNormal();
  filterBetween = new FilterBetween();
  filterFuzzy = new FilterFuzzy();
  filterNormal.setFilter(filterBetween);
  filterBetween.setFilter(filterFuzzy);
  Set<POLICY> endKey1 = map.getFirstKeys();
  List<T> listResult = new ArrayList<T>();
  for (T resMap : mAllList) {
   for (POLICY policy : endKey1) {
    ConcurrentHashMap<String, String> queryMap = map.get(policy);
    if (filterNormal.doFilter(resMap, policy, queryMap) && listResult.contains(resMap)) {
     listResult.add(resMap);
    }
   }
  }
  return listResult;
 }

 public static enum POLICY { NORMAL, BETWEEN, FUZZY }
}

/*======== ======*/
abstract class Filter {

 Filter filter;

 public void setFilter(Filter filter) {
  this.filter = filter;
 }

 public Filter getFilter() {
  return filter;
 }

 abstract boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap);
}
//
class FilterNormal extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.NORMAL.name())) {
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(!resMap.get(entry.getKey()).toString().contains(entry.getValue())) {
     return false;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}
//between
class FilterBetween extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.BETWEEN.name())) {
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(!Pattern.matches(".+?Φ.+?", entry.getValue()))
     throw new UnknownError("Values should be .+?Φ.+? pattern");
    String firstValue = entry.getValue().split("Φ")[0];
    String secondValue = entry.getValue().split("Φ")[1];
    if(resMap.get(entry.getKey()).toString().compareTo(firstValue) < 0
      || resMap.get(entry.getKey()).toString().compareTo(secondValue) > 0) {
     return false;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}
//
class FilterFuzzy extends Filter {

 @Override
 boolean doFilter(Map<String, Object> resMap, POLICY policy, Map<String, String> queryMap) {
  // TODO Auto-generated method stub
  if (policy.name().equalsIgnoreCase(POLICY.FUZZY.name())) {
   String tempStr = resMap.values().toString().replace(" ", "").replace(",", "");
   Iterator<Entry<String, String>> iterator = queryMap.entrySet().iterator();
   while(iterator.hasNext()) {
    Entry<String, String> entry = iterator.next();
    if(tempStr.substring(1, tempStr.length()-1).contains(entry.getValue())) {
     return true;
    }
   }
  } else if (filter != null) {
   return filter.doFilter(resMap, policy, queryMap);
  }
  return true;
 }
}


// k1-k2-v
public class HashHashMap<K1, K2, V> {

    private ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>> k1k2vMap;

    public HashHashMap() {
        this.k1k2vMap = new ConcurrentHashMap<K1, ConcurrentHashMap<K2, V>>();
    }

    public void put(K1 key1, K2 key2, V value) {
        if (k1k2vMap.containsKey(key1)) {
            ConcurrentHashMap<K2, V> k2vMap = k1k2vMap.get(key1);
            k2vMap.put(key2, value);
        } else {
            ConcurrentHashMap<K2, V> k2vMap = new ConcurrentHashMap<K2, V>();
            k2vMap.put(key2, value);
            k1k2vMap.put(key1, k2vMap);
        }
    }

    public Set<K1> getFirstKeys() {
        return k1k2vMap.keySet();
    }

    public V get(K1 key1, K2 key2) {
        ConcurrentHashMap<K2, V> k2_v = k1k2vMap.get(key1);
        return k2_v == null ? null : k2_v.get(key2);
    }

    public ConcurrentHashMap<K2, V> get(K1 key1) {
        return k1k2vMap.get(key1);
    }

    public boolean containsKey(K1 key1, K2 key2) {
        if (k1k2vMap.containsKey(key1)) {
            return k1k2vMap.get(key1).containsKey(key2);
        }
        return false;
    }

    public boolean containsKey(K1 key1) {
        return k1k2vMap.containsKey(key1);
    }

    public void clear() {
        if (k1k2vMap.size() > 0) {
            for (ConcurrentHashMap<K2, V> k2vMap : k1k2vMap.values()) {
                k2vMap.clear();
            }
            k1k2vMap.clear();
        }
    }
}

//
HashHashMap<POLICY, String, String> map = new HashHashMap<CommonLocalQuery.POLICY, String, String>();
  for(int i = 0; i < queryKey.length; i++){
   map.put(POLICY.NORMAL, queryKey[i], queryValue[i]);
  }
List<Map<String, Object>> mTempList = new CommonLocalQuery<HashMap<String, Object>>(

좋은 웹페이지 즐겨찾기