Category Archives: Java

RxJava .reduce()

Observable.from(new Integer[]{1,2,3,4,5}) .reduce(new Func2<Integer, Integer, Integer>() { @Override public Integer call(Integer t1, Integer t2) { return t1+t2; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer t) { // t = 15 } });

Also posted in RxJava | Leave a comment

Observable.zip()

public interface Api {   @GET("x") Observable<X> getX();   @GET("y") Observable<Y> getY();   } Observable.zip(api.getX(), api.getY(), new Func2<X, Y, Pair>() { @Override public Pair call(X x, Y y) { return new Pair(x, y); } }).subscribe(new Action1<Pair>() { @Override public void call(Pair pair) { // you got pair ;) } });

Also posted in Retrofit, RxJava | Leave a comment

RxJava – Observable.interval()

package rxjava;   import java.util.Random; import java.util.concurrent.TimeUnit;   import rx.Observable; import rx.Observer; import rx.functions.Func1; import rx.schedulers.Schedulers;   public class RxInterval {   private final Random random = new Random();   public RxInterval() {   Observable<Integer> observable = Observable .interval(5, TimeUnit.SECONDS) /** * startWith() * do Observable dodawany jest element startowy, który emitowy jest przed pierwszym [...]

Also posted in RxJava | Leave a comment

Spring Annotation Based Single Class Web Application with JSP View Resolver

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView;   @Configuration @EnableWebMvc @Controller public class [...]

Also posted in Spring, jetty | Leave a comment

Spring Annotation Based Single Class Web Application

Works on Servlet 3.0+ containers. Better not mix Configuration classes with Controller classes. It is only for simplicity import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration;   import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc;   @Configuration @EnableWebMvc @Controller public class SpringWebApp implements WebApplicationInitializer {   @RequestMapping("/") @ResponseBody public [...]

Also posted in Spring | Leave a comment

Java 8 Lambda expressions

(parameters) -> expression (parameters) -> {statements;} Examples from Java 8 Lambdas: Functional Programming For The Masses Example 1 ActionListener oneArgument = new ActionListener() { @Override public void actionPerformed(ActionEvent event) { System.out.println("button clicked"); } }; ActionListener oneArgument = event -> System.out.println("button clicked"); Example 2 Runnable noArguments = new Runnable() { @Override void run() { System.out.println("Hello World"); [...]

Posted in Java | Leave a comment

Android – Save / Load state Fragment

public class SomeFragment extends Fragment {   public final static String OBJECT_ATTR = "object"; Data data;   @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {   /** * restore */ if(savedInstanceState != null) { data = savedInstanceState.getParcelable(OBJECT_ATTR); }   /** * using data */ … }   /** * called when [...]

Also posted in Android | Leave a comment

Maven project from archetype – Interactive mode

mvn archetype:generate -Dfilter=org.apache.maven.archetypes: Good ones for starting: maven-archetype-quickstart, maven-archetype-webapp mvn archetype:generate -DarchetypeCatalog=http://repo.maven.apache.org/maven2/archetype-catalog.xml -Dfilter=org.codehaus.mojo.archetypes: Good ones for starting: ejb-javaee6, ejb-javaee7

Also posted in Maven | Leave a comment

jetty-maven-plugin 8.1.15.v20140411

Below a sample config for new version of jetty plugin for maven. <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.15.v20140411</version> <configuration> <webApp> <contextPath>/myapp</contextPath> </webApp> <reload>automatic</reload> <scanIntervalSeconds>1</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8081</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin>

Also posted in Maven | Leave a comment

Install JAR in local repo

Example of installing JACOB library in local repository repo: mvn install:install-file -Dfile=jacob.jar \ -DgroupId=com.jacob \ -DartifactId=jacob \ -Dversion=1.7 \ -Dpackaging=jar \ -DlocalRepositoryPath=repo \ -DcreateChecksum=true one line version: mvn install:install-file -Dfile=jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.7 -Dpackaging=jar -DlocalRepositoryPath=repo -DcreateChecksum=true

Also posted in Maven | Leave a comment