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

kr.kyg.utils.common.collections.KYGMap Maven / Gradle / Ivy

package kr.kyg.utils.common.collections;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 김용건
 * @since 2018-07-06
 */
public class KYGMap {
  private Map map;
  public KYGMap(Object... datas) {
    map = createOfMap(datas);
  }
  public KYGMap(Map map) {
    this.map = map;
  }
  public KYGMap() { this.map = new HashMap<>(); }

  /**
   * KYGMap을 java.util.Map으로 변경
   * @return HashMap
   */
  public Map toMap() {
    return map;
  }
  public LinkedHashMap toLinkedHashMap() { return new LinkedHashMap<>(map); }

  public void put(String key, Object value) {
    this.map.put(key, value);
  }
  public void putAll(Map map) {
    this.map.putAll(map);
  }

  /**
   * 범용 캐스팅 get()
   * @param key - 가져올 맵 key
   * @param defaultValue - 데이터가 없을 경우 출력할 defaultvalue
   * @param classType - 리턴할 형식
   * @return 조건에 맞는 데이터
   */
  public  T get(String key, T defaultValue, Class classType) {
    try {
      return classType.cast(map.get(key));
    } catch (ClassCastException | NullPointerException  ignore) {
      return defaultValue;
    }
  }
  /**
   * 범용 캐스팅 get(). 값이 없을 경우 null을 출력함
   * @param key - 가져올 맵 key
   * @param classType - 리턴할 형식
   * @return 조건에 맞는 데이터
   */
  public  T get(String key, Class classType) {
    return get(key, null, classType);
  }

  /**
   * 데이터를 string 형식으로 가져옴
   * @param key - 가져올 맵 key
   * @param defaultValue - 데이터가 없을 경우 출력할 defaultvalue ( 기본 값: null )
   * @return String형식의 데이터
   */
  public String getString(String key, String defaultValue) {
    return get(key, defaultValue, String.class);
  }
  /**
   * 데이터를 string 형식으로 가져옴
   * @param key - 가져올 맵 key
   * @return String형식의 데이터
   */
  public String getString(String key) {
    return getString(key, null);
  }

  public int getInt(String key, int defaultValue) { return get(key, defaultValue, Integer.class); }
  public int getInt(String key) { return get(key, 0, Integer.class); }

  public boolean isEmpty() {
    return this.map.isEmpty();
  }
  public boolean isNotEmpty() {
    return !this.map.isEmpty();
  }
  public boolean isNull(String key) {
    return (this.map.get(key) == null);
  }
  public boolean isNotNull(String key) {
    return !isNull(key);
  }
  public boolean isType(String key, Class typeClass) {
    if (isNotNull(key)) {
      return key.getClass().getName().equals(typeClass.getName());
    } else {
      return false;
    }
  }

  /**
   * 간단한 맵을 생성하는 메소드
   * @param datas - 추가할 데이터, 홀수는 String, 짝수는 데이터 (Object)
   * @throws NullPointerException - 키값이 null 일 경우 발생
   * @return 생성된 Map
   */
  public static KYGMap createOf(Object... datas) {
    return new KYGMap(datas);
  }
  /**
   * 간단한 맵을 생성하는 메소드
   * @param mapClass - 생성할 map의 클래스
   * @param datas - 추가할 데이터, 홀수는 String, 짝수는 데이터 (Object)
   * @throws NullPointerException - 키값이 null 일 경우 발생
   * @return 생성된 Map
   */
  public static  Map createOfMap(Class mapClass, Object... datas) {
    Map createMap = null;
    try {
      createMap = mapClass.newInstance();
    } catch (InstantiationException | IllegalAccessException e) {
      return null;
    }

    try {
      for (int i=0; i) datas[i]);
          i++;
        } else {
          createMap.put(datas[i].toString(), datas[i+1]);
        }
      }
    } catch (ArrayIndexOutOfBoundsException ignore) {
      // 입력된 데이터가 홀수일 경우 마지막 내용은 null로 채워서 넣는다
      createMap.put(datas[datas.length - 1].toString(), null);
    }

    return createMap;
  }
  /**
   * 간단한 맵을 생성하는 메소드
   * @param datas - 추가할 데이터, 홀수는 String, 짝수는 데이터 (Object)
   * @throws NullPointerException - 키값이 null 일 경우 발생
   * @return 생성된 Map
   */
  public static Map createOfMap(Object... datas) {
    return createOfMap(HashMap.class, datas);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy