Skip to content

Instantly share code, notes, and snippets.

View yenerm's full-sized avatar

Murat Yener yenerm

  • Google
  • San Francisco
View GitHub Profile
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class ListWithTrash <T>(
private val innerList: MutableList<T> = ArrayList<T>()
) : MutableCollection<T> by innerList {
var deletedItem : T? = null
override fun remove(element: T): Boolean {
deletedItem = element
return innerList.remove(element)
@yenerm
yenerm / OptionalSample.java
Created June 16, 2020 18:56
Optional Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
Optional<String> opt = Optional.of("Java 8");
if (opt.isPresent()){ //true
//...
}
opt = Optional.ofNullable(null);
if (opt.isEmpty()){ //true
@yenerm
yenerm / ComparatorSample.java
Created June 16, 2020 18:55
Comparator Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
import static java.util.Comparator.comparing;
comparing(Person::getFirstname).thenComparing(Person::getSurname);
@yenerm
yenerm / StreamSample.java
Created June 16, 2020 18:54
Stream Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
List<person> list = Arrays.asList(personArray);
Stream<Person> stream1 = list.stream();
//or
Stream<Person> stream2 = Stream.of(personArray);
//or
@yenerm
yenerm / PeriodSample.java
Created June 16, 2020 18:53
Period Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 →
LocalDate now = LocalDate.now();
LocalDate androidBeta = LocalDate.of(2020, 6, 10);
Period period = Period.between(androidBeta, now);
@yenerm
yenerm / OffsetDateTimeSample.java
Created June 16, 2020 18:52
OffsetDateTime Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
ZoneOffset zoneOffSet= ZoneOffset.of("+03:00");
OffsetDateTime offsetDateTime = OffsetDateTime.now(zoneOffSet);
System.out.println(offsetDateTime);
//2020-06-08T23:50:17.315+03:00
@yenerm
yenerm / ZonedDateTimeSample.java
Created June 16, 2020 18:50
ZonedDateTime Sample
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 →
ZonedDateTime currentDate = ZonedDateTime.now();
System.out.println("the current zone is "+ currentDate.getZone());
//the current zone is America/Los_Angeles
ZoneId anotherZone = ZoneId.of(“Europe/Istanbul”);
ZonedDateTime currentDateInIstanbul = currentZone.withZoneSameInstant(anotherZone);
//2020-06-08T23:50:00.159+03:00[Europe/Istanbul]
@yenerm
yenerm / LocalDateLocalTime.java
Created June 16, 2020 18:49
LocalDate and LocalTime
// Current date
LocalDate date = LocalDate.now();
// Current time
LocalTime time = LocalTime.now();
// Date of Android 11 beta launch
LocalDate date2 = LocalDate.of(2020, 6, 10); //yyyy,MM,dd
@yenerm
yenerm / build.gradle
Last active June 16, 2020 18:47
Java 8 desugaring
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
android {
defaultConfig {
//Only required when setting minSdkVersion to 20 or lower
multiDexEnabled true
}
compileOptions {
@yenerm
yenerm / GenericFun.kt
Created May 17, 2020 23:00
Generic Function in Kotlin
fun <T> printType(classType: Class<T>) {
print(classType::class.java)
}