졸업프로젝트 week19
이번주는 팀원 모두 모여 프로젝트를 진행하였다. 지난주 교수님께서 피드백 해주신 ContentValues에 있는 수많은 put 함수에 대한 내용을 중점적으로 다루고, 리팩토링을 시도하여 빌드해보기로 하였다.
ContentValues 분석
ContentValues 내의 인스턴스 별로 오버로딩 되어 있는 put 함수를 분석하기에 앞서, ContentValues의 역할을 찾아보았다. 링크
위와 같이 ContentValues 클래스는 ContentResolver가 사용하기 위한 값의 집합이다. 즉, ContentResolver가 사용하는 값들을 담은 객체라고 이해하였다. 이는 DB에 삽입, 수정, 삭제 등의 작업을 할 때 사용하기 위한 값들을 객체형태로 사용하기 위해 ContentValues라는 클래스를 사용한다고 확인하였다.
ContentProviderOperation.java 내의 withValue 함수는 아래와 같다.
private final ContentValues mValues;
public Builder withValue(String key, Object value) {
if (mType != TYPE_INSERT && mType != TYPE_UPDATE && mType != TYPE_ASSERT) {
throw new IllegalArgumentException("only inserts and updates can have values");
}
if (mValues == null) {
mValues = new ContentValues();
}
if (value == null) {
mValues.putNull(key);
} else if (value instanceof String) {
mValues.put(key, (String) value);
} else if (value instanceof Byte) {
mValues.put(key, (Byte) value);
} else if (value instanceof Short) {
mValues.put(key, (Short) value);
} else if (value instanceof Integer) {
mValues.put(key, (Integer) value);
} else if (value instanceof Long) {
mValues.put(key, (Long) value);
} else if (value instanceof Float) {
mValues.put(key, (Float) value);
} else if (value instanceof Double) {
mValues.put(key, (Double) value);
} else if (value instanceof Boolean) {
mValues.put(key, (Boolean) value);
} else if (value instanceof byte[]) {
mValues.put(key, (byte[]) value);
} else {
throw new IllegalArgumentException("bad value type: " + value.getClass().getName());
}
return this;
}
위의 함수에서 ContentValues 타입의 mValues의 put 함수를 인스턴스별로 호출하는 것을 확인하였고, 이에 따라 ContentValues의 클래스를 확인해보았다. ContentValues.java 의 put 함수들 코드는 아래와 같다.
private HashMap<String, Object> mValues;
public void put(String key, String value) {
mValues.put(key, value);
}
/**
* Adds all values from the passed in ContentValues.
*
* @param other the ContentValues from which to copy
*/
public void putAll(ContentValues other) {
mValues.putAll(other.mValues);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Byte value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Short value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Integer value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Long value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Float value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Double value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, Boolean value) {
mValues.put(key, value);
}
/**
* Adds a value to the set.
*
* @param key the name of the value to put
* @param value the data for the value to put
*/
public void put(String key, byte[] value) {
mValues.put(key, value);
}
/**
* Adds a null value to the set.
*
* @param key the name of the value to make null
*/
public void putNull(String key) {
mValues.put(key, null);
}
ContentOperation에서 ContentValues 타입의 mValues에 put을 호출해서 사용하는데, 실제 선언되어 있는 부분을 확인해보면 HashMap<String, Object> 의 형태로 선언된 mValues에 put을 하여 저장 하는 것으로 보였다. 위의 put 함수들이 인스턴스 별로 쪼개어져 오버로딩 되어 있는 이유는 아직 찾지 못했다. 그러나 HashMap을 직접 테스팅 해본결과 HashMap<String, Object> 형태로 선언된 해시맵에 값으로 String, Byte, Short, Long 등의 어떠한 형태든 상관없이 put(String, Object) 형태의 함수 단 하나로 충분히 사용가능한 것을 확인하였다. 따라서 위의 코드에서 인스턴스 별로 오버로딩 되어 있는 put 함수들을 없에고 Object타입의 put 함수 하나만 남겨서 빌드해보기로 하였다.
또한 위의 코드가 안드로이드 버젼 9(파이) 까지는 계속 유지되었으나, 현재 개발 중인 안드로이드 버젼 10 (Q)에서는 변경 사항을 확인하였다. 이하 내용 참고