Skip to content

Instantly share code, notes, and snippets.

View smallufo's full-sized avatar
🏠
Working from home

smallufo

🏠
Working from home
  • Taiwan
View GitHub Profile
@smallufo
smallufo / RxOkHttp.java
Last active August 29, 2015 14:15
Listening twitter keyword , implemented by OkHttp + RxJava's Observables
/**
* Created by smallufo on 2015-02-22.
*/
package twitter;
import com.squareup.okhttp.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;
import rx.Subscriber;
@smallufo
smallufo / TrackerByTwitter4j.java
Created February 22, 2015 06:23
Listening to twitter keywords , implemented by Twitter4j and RxJava
/**
* Created by smallufo on 2015-02-22.
*/
package twitter;
import rx.Observable;
import rx.subjects.PublishSubject;
import rx.subjects.Subject;
import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder;
@smallufo
smallufo / DataDao.java
Created March 27, 2015 09:41
rxjava scroll database example by hibernate. (pity , JPA doesn't support scroll)
public Observable<Data> toObservable(int batch) {
return Observable.create(new Observable.OnSubscribe<Data>() {
@Override
public void call(Subscriber<? super Data> subscriber) {
Session session = ((Session) emp.get().getDelegate()).getSessionFactory().openSession();
Query query = session.createQuery("select d from Data d order by id asc");
query.setFetchSize(batch);
query.setReadOnly(true);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
@smallufo
smallufo / PaginationBar.java
Last active March 22, 2016 08:17
slightly modified PaginationBar
/**
* Created by smallufo on 2016-03-21.
*/
package destiny.vaadin.components;
import com.vaadin.server.FontAwesome;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.themes.ValoTheme;
@smallufo
smallufo / 1_pure.java
Last active December 29, 2017 17:01
From J to K
// Write Once Read Never
Map<Double , Tuple2<Star , StationaryType>> map = stars
.stream()
.flatMap(star -> {
List<Tuple2<Double , StationaryType>> list = getPeriodStationary(star, fromGmt, toGmt, starPositionImpl);
return list.stream().map(t -> Tuple.tuple(t.v1() , star , t.v2()));
}).collect(Collectors.toMap(Tuple3::v1, t -> Tuple.tuple(t.v2() , t.v3()) , (v1, v2) -> v1 , TreeMap::new));
return map.entrySet().stream()
.map(entry -> Tuple.tuple(entry.getKey() , entry.getValue().v1() , entry.getValue().v2()))
public interface IBookService {
List<Tuple2<LocalDate , Book>> findBooks(User user , LocalDate from , LocalDate to);
}
public interface IBookService {
List<Tuple2<LocalDate , Book>> findBooks(User user , LocalDate from , LocalDate to);
default List<Tuple3<LocalDate , User , Book>> findUserBooks(List<User> users , LocalDate from , LocalDate to) {
Map<LocalDate , Tuple2<User , Book>> map = users.stream()
.flatMap(user -> {
List<Tuple2<LocalDate , Book>> list = findBooks(user , from , to);
return list.stream().map(t -> Tuple.tuple(t.v1 , user , t.v2));
}
interface IBookService {
fun findBooks(user: User, from: LocalDate, to: LocalDate): List<Tuple2<LocalDate, Book>>
fun findUserBooks(users: List<User>, from: LocalDate, to: LocalDate): List<Tuple3<LocalDate, User, Book>> {
val map = users.stream()
.flatMap { user ->
val list = findBooks(user, from, to)
list.stream().map { t -> Tuple.tuple(t.v1, user, t.v2) }
}.collect<TreeMap<LocalDate, Tuple2<User, Book>>, Any>(Collectors.toMap(
interface IBookService {
fun findBooks(user: User, from: LocalDate, to: LocalDate): List<Tuple2<LocalDate, Book>>
fun findUserBooks(users: List<User>, from: LocalDate, to: LocalDate): List<Tuple3<LocalDate, User, Book>> {
return users.flatMap { user ->
findBooks(user , from , to).map { it -> Tuple3(it.v1 , user , it.v2) }
}.sortedBy { triple -> triple.v1 }
}
}
LocalDateTime getNextFullMoon(LocalDateTime from);
default List<LocalDateTime> getYearlyFullMoons(int year) {
List<LocalDateTime> result = new ArrayList<>();
LocalDateTime from = LocalDateTime.of(year , 1 , 1 , 0 , 0);
while (from.getYear() < year+1) {
LocalDateTime fullMoon = getNextFullMoon(from);
if (fullMoon.getYear() == year)