List<String> list1; final List<String> list2;
val list1: List<String>? val list2: List<String?>? val list3: List<String> val list4: List<String?> val list5: MutableList<String>? val list6: MutableList<String?>? val list7: MutableList<String> val list8: MutableList<String?> var list9: List<String>? var list10: List<String?>? var list11: List<String> var list12: List<String?> var list13: MutableList<String>? var list14: MutableList<String?>? var list15: MutableList<String> var list16: MutableList<String?>
val test = Random().nextBoolean() val list1 = if (test) null else listOf("") val list2 = if (test) null else listOf(null, "") val list3 = listOf("") val list4 = listOf(null, "") val list5 = if (test) null else mutableListOf("") val list6 = if (test) null else mutableListOf(null, "") val list7 = mutableListOf("") val list8 = mutableListOf(null, "") var list9 = list2?.filterNotNull() var list10 = list2 var list11 = list2?.filterNotNull() ?: emptyList() var list12 = list2 ?: emptyList() var list13 = list2?.filterNotNull()?.toMutableList() var list14 = list2?.toMutableList() var list15 = list2?.filterNotNull()?.toMutableList() ?: mutableListOf() var list16 = list2?.toMutableList() ?: mutableListOf()
data class Schedule( val delay: Int, val delayTimeUnit: TimeUnit = TimeUnit.SECONDS, val rate: Int? = null, val rateTimeUnit: TimeUnit = TimeUnit.SECONDS, val run: () -> Unit ) fun usage() { Schedule(1) { println("Delay for second") } Schedule(100, TimeUnit.MILLISECONDS) { println("Delay for 100 milliseconds") } Schedule(1, rate = 1) { println("Delay for second, repeat every second") } }
@Repository interface PayerRepository : CrudRepository<Payer, Int> { fun findByApprenticeId(id: Int): List<Payer> } @Repository interface AttendanceRepository : CrudRepository<LessonAttendance, LessonAttendance.ID> { fun findByDateBetween(from: Date, to: Date): List<LessonAttendance> } fun AttendanceRepository.byMonth(month: Date): List<LessonAttendance> { val from = month.truncateToMonth() val to = month.addMonths(1).subtractDays(1) return findByDateBetween(from, to) } // 10 inline fun <reified T, ID: Serializable> CrudRepository<T, ID>.find(id: ID): T { return findOne(id) ?: throw ObjectNotFound(id, T::class.qualifiedName) }
fun isJournalBlocked(date: Date, forMonth: Date) = forMonth <= date.subtractMonths(1).subtractDays(10) // 20 fun Date.subtractMonths(amount: Int): Date = DateUtils.addMonths(this, -amount) // 8 fun Date.subtractDays(amount: Int): Date = DateUtils.addDays(this, -amount)
public boolean isJournalBlocked(Date date, Date forMonth) { return date.compareTo(DateUtils.addDays(DateUtils.addMonths(forMonth, -1), -1)) <= 0; }
interface History<out T> { val begin: Date var end: Date? fun historyOf(): T fun containsMonth(date: Date): Boolean { val month = date.truncateToMonth() return begin <= month && (end == null || month < end) } } fun <T> SortedMap<Date, out History<T>>.fix() { removeRepeatedNeighbors() val navigableMap = TreeMap<Date, History<T>>(this) values.forEach { it.end = navigableMap.higherEntry(it.begin)?.value?.begin } } private fun <T> SortedMap<Date, out History<T>>.removeRepeatedNeighbors() { var previousHistory: T? = null for (history in values.toList()) { if (history.historyOf() == previousHistory) { remove(history.begin) } else { previousHistory = history.historyOf() } } } //usage: fun setGroup(from: Date, group: ClassGroup) { val history = GroupHistory( this, group, from.truncateToMonth(), null ) groupsHistory[history.begin] = history groupsHistory.fix() this.group = groupsHistory.getValue(groupsHistory.lastKey()).group }
val apprentices: List<ApprenticeDTO> = apprenticeRepository.findAll() .map(::ApprenticeDTO) .sortedWith(compareBy({ it.lastName }, { it.firstName }))
List<ApprenticeDTO> apprentices = StreamSupport.stream( apprenticeRepository.findAll().spliterator(), false ).map(ApprenticeDTO::new) .sorted(Comparator.comparing(ApprenticeDTO::getLastName) .thenComparing(Comparator.comparing(ApprenticeDTO::getFirstName))) .collect(Collectors.toList());
val attendances: Map<Pair<Date, Int>, Int> attendances = attendanceRepository .byMonth(month) .groupBy { it.date to it.group.id } .mapValues { it.value.count() } .toMap()
Map<Pair<Date, Integer>, Integer> attendances = attendanceRepository .byMonth(month) .stream() .collect(Collectors.groupingBy((it) -> new Pair<>(it.getDate(), it.getGroup().getId()))) .entrySet() .stream() .map(entry -> new Pair<>(entry.getKey(), entry.getValue().size())) .collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
fun rentForGroup(month: Date, group: ClassGroup): Int { val hall = group.hall val hallRent = hall.rent(month) return when (hallRent) { is Monthly -> hallRent.priceForMonth() / hall.groups(month).size is PercentOfRevenue -> hallRent.priceForMonth(creditForGroup(month, group)) is Hourly -> hallRent.priceForLessons(group.monthLessons(month)) } }
public int rentForGroup(Date month, ClassGroup group) { Hall hall = group.getHall(); Rent hallRent = hall.rent(month); if (hallRent instanceof Monthly) { return ((Monthly) hallRent).priceForMonth() / hall.groups(month).size(); } else if (hallRent instanceof PercentOfRevenue) { return ((PercentOfRevenue) hallRent).priceForMonth(creditForGroup(month, group)); } else if (hallRent instanceof Hourly) { return ((Hourly) hallRent).priceForLessons(group.monthLessons(month)); } else { throw new UnsupportedOperationException(); } }
inline fun <reified E : Throwable> assertFail(expression: () -> Unit) { try { expression() Assert.fail("expression must fail with ${E::class.qualifiedName}") } catch (e: Throwable) { if (e !is E) { throw e } } } @Test fun greenTest() { assertFail<ArrayIndexOutOfBoundsException> { arrayOf(1, 2)[3] } }
inline fun <reified T, ID: Serializable> CrudRepository<T, ID>.find(id: ID): T { return findOne(id) ?: throw ObjectNotFound(id, T::class.qualifiedName) }
val email = """^([_A-Za-z0-9-+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,}))?$"""
String email = "^([_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,}))?$"
Source: https://habr.com/ru/post/337002/
All Articles