Guava: Google Core Libraries for Java 간단한 사용방법.
Guava: Google Core Libraries for Java
- Base
- Objects.equal() : equal 비교시 null 체크를 하지 않아도 된다.
- Objects.hashCode() : hash코드 생성을 보다 쉽게 만들 수 있다.
- Objects.toStringHelper() : toString객체를 보다 쉽게 만들 수 잇다.
-> 일반적인 코드 :
public class Book {
private String title;
private String writer;
private int price;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
if (price != book.price) return false;
if (title != null ? !title.equals(book.title) : book.title != null) return false;
return !(writer != null ? !writer.equals(book.writer) : book.writer != null);
}
@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;
result = 31 * result + (writer != null ? writer.hashCode() : 0);
result = 31 * result + price;
return result;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", writer='" + writer + '\'' +
", price=" + price +
'}';
}
}
-> Guava를 이용한 코드
public class Book {
private String title;
private String writer;
private int price;
public Book(String title, String writer, int price) {
this.title = title;
this.writer = writer;
this.price = price;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
Preconditions.checkNotNull(title);
this.title = title;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Objects.equal(this.title, book.title) && Objects.equal(this.writer, book.writer)
&& this.price == book.price;
}
@Override
public int hashCode() {
return Objects.hashCode(this.title, this.writer, this.price);
}
@Override
public String toString() {
return Objects.toStringHelper(this)
.add("title", title)
.add("writer", writer)
.add("price", price)
.toString();
}
}
작성하다보니 Objects.toStringHelper 가 deprecated가 되었네요.. 이건 MoreObject.toStringHelper()를 이용하면 됩니다.
- Preconditions : 객체 정보의 null과 같은 정보가 있는지를 미리 확인하여 exception을 낸다.
-> 일반적인 코드
public void setTitle(String title) {
if(title == null) {
throw new NullPointerException("title must not be null!");
}
this.title = title;
}
-> Preconditions를 이용
public void setTitle(String title) {
Preconditions.checkNotNull(title);
this.title = title;
}
- Joiner : Collections객체들이나 array객체들의 정보들을 string으로 변경해준다.
List names = new ArrayList<>();
names.add("test1");
names.add("test2");
names.add("test3");
Joiner.on(", ").join(names); // test1, test2, test3
- Splitter : Joiner와 반대로 split 캐릭터를 기준으로 Iterator객체로 변환하여 준다.
Iterator names = Splitter.on(",")
.trimResults()
.omitEmptyStrings()
.split("test1, test2, test3").iterator();
names.next(); //test1
names.next(); //test2
names.next(); //test3
- Collections
Lists.newArrayList();
Sets.newHashSet();
Maps.newHashMap();
Set<String> nameSet = Collections.unmodifiableSet(new LinkedHashSet<String>(Arrays.asList("test1", "test2", "test3")));
Set<String> nameSet = ImmutableSet.of("test1", "test2", "test3");
Map<String, Integer> mapValue1 = new LinkedHashMap<>();
mapValue1.put("test1", 1);
mapValue1.put("test2", 2);
mapValue1.put("test3", 3);
Map<String, Integer> unModifiedMap = Collections.unmodifiableMap(mapValue1);
Map<String, Integer> mapValue2 = ImmutableMap.<String, Integer>builder()
.put("test1", 1).put("test2", 2).put("test3", 3)
.build();
Map<String, Integer> mapValue3 = ImmutableMap.of("test1", 1, "test2", 2, "test3", 3);
- ImmutableCollection
- ImmutableList
- ImmutableSet
- ImmutableSortedSet
- ImmutableMap
- ImmutableSortedMap
- ImmutableMultiset
- ImmutableSortedMultiset
- ImmutableMultimap
- ImmutableListMultimap
- ImmutableSetMultimap
- ImmutableBiMap
- ImmutableClassToInstanceMap
- ImmutableTable
- Functional programming
public interface Function {
@Nullable T apply(@Nullable F input);
}
Function<Integer, Double> calcMileToKillometer = new Function<Integer, Double>() {
@Override
public Double apply(@Nullable Integer input) {
return input * 1.60934;
}
};
double miles = calcMileToKillometer.apply(1);
List<Book> bookList = ImmutableList.of(new Book("Android", "test", 100), new Book("Java", "test2", 200), new Book("Object", "test3", 300));
List<Book> androidBookList = ImmutableList.copyOf(Collections2.filter(bookList, new Predicate<Book>() {
@Override
public boolean apply(@Nullable Book input) {
return "Android".equals(input.getTitle());
}
}));
List<String> titleList = ImmutableList.copyOf(Collections2.transform(bookList, new Function<Book, String>() {
@Override
public String apply(@Nullable Book input) {
return input.getTitle();
}
}));
'Android' 카테고리의 다른 글
[Android] Dagger2에서 @Singleton scope및 custom scope annotation이용 (0) | 2015.11.10 |
---|---|
Setting Up Android CheckStyle in Android Studio (0) | 2015.11.10 |
[Android] MVC, MVVM, MVP (0) | 2015.10.26 |
[Android] Android 개발에서 Dagger2이용해보기. (1) | 2015.10.15 |
[Android] Fragment에서 onActivityResult 결과 받기. (1) | 2015.10.02 |