티스토리 뷰

Programming/유용한 라이브러리

Guava

Albothyl 2019. 2. 7. 16:22

Guava

- 구글이 개발한 자바 오픈소스 유틸리티 라이브러리.

  • Cache
  • Collection
  • Functional
  • String
  • Others 

1. Local Cache

  • sample code

private LoadingCache<Long, String> sampleCache = CacheBuilder.newBuilder()

.maximumSize(100000)

.expireAfterWrite(1, TimeUnit.MINUTES)

.build(new CacheLoader<Long, String>() {

@Override

public String load(Long targetId) {

return targetRepository.findById(targetId);

}

});


public void refresh(Long id) {

sampleCache.refresh(targetId)

}


public String get(Long id) {

sampleCache.getUnchecked(id)

}


  • b. 캐싱 정책

refreshAfterWrite(long, TimeUnit)

- 특정 시간이후 자동으로 데이터를 다시 loading.

- refreshAfterWrite는 데이터가 로딩할때까지 예전 값을 리턴.


expireAfterAccess(long, TimeUnit) 

- access (read) 이후 특정 시간 후 expire.


expireAfterWrite(long, TimeUnit)

- write 이후 특정 시간 후 expire.


2. Collection

  • Lists, Maps, Sets ...

- new ArrayList<>(); -> Lists.newArrayList();

- new HashMap<>();   -> Maps.newHashMap();

- new HashSet<>();   -> Sets.newHashSet();


  • MultiMap

- 같은 key가 들어오면 value가 over write하지 않고, list에 추가된다.

Multimap<Long, String> multiMap = ArrayListMultimap.create();

multiMap.put("id_1", "value_1");

multiMap.put("id_1", "value_2");

multiMap.put("id_3", "value_3");


id_1 -> [value_1, value_2]

id_3 -> [value_3]


  • Bimap

- 값의 유일성을 보장하는 map

BiMap<String, String> biMap = HashBiMap.create();

biMap.put("id_1", "value_1");

// biMap.put("id_2", "value_1"); ->> 동일한값이 들어오면 IllegalArgumentException 발생

biMap.put("id_3", "value_3");


  • MultiTable

- key가 2개(rowKey, columnKey)인 map

Table<String, String, Integer> multiTable = HashBasedTable.create();

multiTable.put("rowKey_1", "columnKey_1", "value_1")

multiTable.put("rowKey_1", "columnKey_2", "value_2")

multiTable.put("rowKey_2", "columnKey_1", "value_3")


multiTable.get("rowKey_1", "columnKey_1") >> "value_1"

multiTable.get("rowKey_1", "columnKey_2") >> "value_2"

multiTable.get("rowKey_2", "columnKey_1") >> "value_3"


3. Java8 이하 버전에서 Java8의 기능을 사용

  • Function, Predicate, Filter ...


4. HashCode

  • sample code

@Override

public int hashCode() {

return Objects.hashCode(foo, bar);

}


5. CompareTo

  • sample code

@Override

public int compareTo(final GuavaExample o) {

return ComparisonChain.start().compare(foo, o.foo).compare(bar, o.bar).result();

}


6. CheckArgument

  • sample code

if (count <= 0) {

    throw new IllegalArgumentException("must be positive: " + count);

}


-> checkArgument(count > 0, "must be positive: %s", count);


7. Joiner

  • sample code 1

List<String> stringList = Lists.newArrayList("value_1", null, "value_2", "value_3");

Joiner joiner = Joiner.on(", ").skipNulls();

String values = joiner.join(stringList);

>> "value_1, value_2, value_3"


  • sample code 2

Map<String, String> map = Maps.newHashMap();     

map.put("key_1", "value_1");

map.put("key_2", "value_2");

map.put("key_3", "value_3");

String values = Joiner.on("&").withKeyValueSeparator("=").join(map);

>> "key_1=value_1&key_2=value_2&key_3=value_3"





참고:


'Programming > 유용한 라이브러리' 카테고리의 다른 글

JsonPath  (0) 2020.04.03
MapStruct  (0) 2019.08.30
Guava AsyncEventBus  (0) 2019.02.07
Flyway 설정  (0) 2019.01.18
Apache HttpClient  (1) 2018.07.21
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함