スキップしてメイン コンテンツに移動

投稿

2013の投稿を表示しています

Java: Shuffling Elements in Array

I will show you java code for shuffling order of elements in array randomly. Only one thing you should notice is generating random is quite sensitive problem in a precise sense. I use java Random class which is provided by JDK. However if you need to use more proper randomness, you may implement class for generate random number sequence yourself. Anyway, the code is below. package com.dukesoftware.utils.math; import java.util.Random; public class Shuffler { private final Random random; public Shuffler(Random random) { this.random = random; } public void shuffle(int[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i int swap = a[i]; a[i] = a[j]; a[j] = swap; } } public void shuffle(double[] a) { for (int i = a.length - 1; i > 0; i--) { int j = random.nextInt(i + 1); // 0 <= j <= i double swap =

Javascript: Lazy Load Image

I realized that most of the modern browser download the image which is set on src attribute even if the img element is hidden. This sometimes goes to performance problem because even if there are a lot of hidden image which are only shown user interacts, all images are downloaded initial page load! So I wrote a simple javascript for reflesh the src attribute and download image only when user interacts somethig on window - like press button etc. I know you can use JQuery LazyLoad plugin , but I would like to take much more faster solution - no external javascript. Here is the demo. When you push the button, the image is loaded lazyly Load image Here is the key method. (maybe you can check the logic by viewing the src directly though.) Replace the src attribute with data-original attribute i the img tag. function loadImage(elem) { if(elem.attr("src") != elem.attr("data-original")) { elem.attr("src", elem.attr("data-original&qu

Drag & Drop Image File onto Java Swing Component

In this post I will show you how to achieve drag and drop image file onto Java Swing Component. In short, implment TransferHandler which can transfer image file. Code Here is the code I wrote. I defined common closure interface in order to give implementation flexibility. The exec method of imageAcceptor will be called with Image object when drag & drop is succeeded. package com.dukesoftware.utils.swing.drag; import java.awt.Image; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.imageio.ImageIO; import javax.swing.TransferHandler; import com.dukesoftware.utils.common.Closure; public class ImageFileTransferHandler extends TransferHandler { private final static Set<String> SUPPORTED_SUFIXES = new HashSet<>(Arr

Java Customize Serialization - Using Externalizable

I will show you how to customize Java serialization (Externalization), performance comparison default serialization vs my externalization implmentation in this post. In the most cases, you don't have to care about the serialization performance of default implementation. But the default implementation uses reflection so it might be a problem if you develop performance critical application. Externalizable Implement your own serialization is very simple. Just implements Externailizable interface into the class. Here is the example code - simple pojo class and TreeMap which implements Externalizable. package com.dukesoftware.demos.externalizable; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.ArrayList; import java.util.List; public class ExternalizableClass implements Externalizable{ private String field1; private Double field2; private List<String> field3; public S

Render FPS Text on Java Swing Component

In the previous post , I showed helper class for 2D animation on Java Swing component. If we can measure the frame rate of animation, it is very great, isn't it? In this post, I will show you that kind of class - rendering FPS text on swing component. Showing code is fast and snappy :p Here is the code - count frame per 1 sec. package com.dukesoftware.utils.awt.graphics; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.Graphics; import java.text.DecimalFormat; public class FPSText { protected static final String FPS = "FPS: "; private static final int FONT_SIZE = 24; private static final int FONT_STYLE = Font.BOLD; private static final String FONT_NAME = "SansSerif"; protected final static Color COLOR = new Color(0.55f,0.55f,0.55f); protected double frameCount; protected final DecimalFormat format = new DecimalFormat("####.00"); protected String fpsText; protected fin

Java 2D Animation on Swing

In this post, I will show you some tips (or tools) for 2D animation on Java Swing component. There are 2 ways to rendering animation on Swing compinent - Thread vs Timer. I think Swing rendering is based on single thread model, so we should use Timer. But I have used separate thread for executing animation and it works fine, so should be fine for not critical application. Animator class Ok then, I can show you a simple animator canvas class I created. The class takes Model2D for actual animate model and render it. One thing you should remember - AnimateCanvas calculate model of animation (coodinates or position or whatever) on animation rendering process, so if the calculation takes takes log time, the animation cannot meet the frame per second passed to constructor. package com.dukesoftware.utils.swing.others; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.aw

Automated Testing: Pairwise Testing, Mutation Testing, CI, TDD

I Introduced basic techniques for automated testing in the following posts. Unit Test by JUnit Parameterized Test Mock Test Theory Test In this post, I mention some hints for software testing. Pairwise Testing It is unrealistic to test all the combination of parameters because the number of the tests increase explosively. Pairwise Testing aims to redeuce the number of tests with enough test quality and coverage by providing effective and enough combination of test parameters. This MSDN: Pairwise Testing in the Real World: Practical Extensions to Test-Case Scenarios helps your understanding Pairwise Testing. The article also introduces the tool for Pairwise Testing. Mutation Testing Change the logic in the test target code (for example, change reverse an inequality sign in the "if" condition check), and check if the test result is changed (= the test case can detect the code change). Mutation Testing is "Test for test" - verifying the effectiveness of

Automated Testing: Theory Test

Theory test is relatively new feature in JUnit testing. In theory test, we define "theory" for parameter combination and filter them by "assume". Then only tests passed parameters by assume. Let's show you some example theory test code. In Junit test, we put @RunWith(Theories.class) annotation on test class for deining theory test. put @DataPoints for parameter data set. The code below shows how theory test works in JUnit. Actually this is not good example because there is no advantedges compared with parameterized test in this case. package com.dukesoftware.exchangerate.service; import junit.framework.Assert; import org.junit.After; import org.junit.BeforeClass; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; import org.mockito.Mockito; import com.dukesoftware.exchangerate.api.ExchangeRateApi; import com.dukesoftware.

Automated Testing: Mock Test

Today's topic is Mock Test . In the previous posts, I introduced bunch of test cases but all of target class is class which hits actual service or production environment. However in the real world, in many cases we cannot simply use actual service class. Isolate from actual production service. ex. storing DB, calling external environment API. External API is too slow and we don't have to use acual data from the API but just would like to test clsasses which uses the API. Would like to controla data from API. Would like to test internal logic. You can use Mock Object, which we can control behaviour of class, for the above cases. There are a lot of Mock libraries (especially in Java). In this post, we use Mockito for explanation. You just downalod mockito-all-x.x.x.jar and include it to classpath, then you can use Mockito. Mockito Example Code I think you can easily understand how to use Mockito from the below code example. package com.dukesoftware.exchangerate.s

Automated Testing: Parameterized Test

In the previous post , I introduced basic of Unit Testing. Next I introduce parameterized testing. Before explaning parameterized testing, let's define a simple service class which uses ExchangeRateApi calss. package com.dukesoftware.exchangerate.service; import java.util.HashMap; import java.util.Map; import com.dukesoftware.exchangerate.api.ExchangeRateApi; import com.dukesoftware.exchangerate.api.Rate; public class PriceCalculator { private final ExchangeRateApi api; private final Map<String, Rate> map = new HashMap<>(); private final static double FEE_RATE = 0.05; public PriceCalculator(ExchangeRateApi api) { this.api = api; } public void initialize() { } public void shutdown() { this.map.clear(); } public double calculatePrice(double price, String ccy1, String ccy2) { Rate rate = this.map.get(ccy1+":"+ccy2); if(rate == null) { // cac

Automated Testing: Unit Test by JUnit

Unit testing From this post, I will explain basic of automated testing. In this post, I will show you very very basic of unit testing using JUnit. We use YahooExchangeRateApi as testing target code introduced in http://dukesoftware00.blogspot.com/2013/10/java-get-exchange-rate-from-yahoo.html . Unit Test class & Annotaions The first of first, write simple unit testing code for YahooExchangeRateApi class. A few things you should remenber: Add @Test annotation to test method. Use @BeforeClass for execute something *once* before actual all tests defined in the test class. Use @Before for execute something before actual *each* tests defined in the test class. Use @AfterClass and @After are simply opposite meaning of @BeforeClass and @Before. e.g. executed after test methods. You can test exception thrown by @Test(expected=Exception.class) Yeah that's all! Let's see the actual test class code.... package com.dukesoftware.exchangerate.api; import static junit.fra

Java: Get Exchange Rate From Yahoo Finance Api

In this post, I will show you how to retrieve currency exchange rate from yahoo finance api by Java program. There are some apis for getting exchange rate - reuters, bloomberg, yahoo finance etc. If you don't have to get high-ratency realtime exchange rate, the most simple and free api - yahoo finance api - is enough. Okay, here is the actual code. The api returns exchange rate as a Rate object. package com.dukesoftware.exchangerate.api; import static java.lang.String.format; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; public class YahooExchangeRateApi implements ExchangeRateApi { // url for yahoo finance api private final static String URL_FORMAT = "http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=%s%s=X"; public Rate getRate(String ccy1, String ccy2) { assertNotNull(ccy1, "

Get Root Cause of Java Exception

I think sometimes you would like to simply extract root cause of exception. The following method will help you. public final static Throwable getRootCause(Throwable t){ for(Throwable s = t.getCause() ;s != null;){ s = t.getCause(); t = s; } return t; }

Create Stack Trace String from Java Exception

Maybe many times you would like to generate stack trace string from Exception thrown. The code below will help you! public final static String createStackTraceString(Exception e){ StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw, false); e.printStackTrace(writer); return sw.toString(); }

File Clean Up and Archiving Linux Command Tips

If you would like to delete files whose name ends with "batch.log" older than 30 days, Use the following command. find /var/log/ -name "*batch.log" -mtime +30 -type -f -delete If you would like to archive files whose name ends with "batch.log" older than 30 days, Use the following command. The archived file will have timestamp at the end of the filename. find /var/log/ -name "*batch.log" -mtime +30 -type -f -pront0 | tar -czvf /var/log/batch.log.tar.gz.`date +\%Y%m%d%H%M%S` -T - --null --remove-files

Solve Local Capistrano Deployment Issue

Local Capistrano Deployment Issue I have an inssue when deploying PHP application on "local" machine (e.g. deploying app to same machine where executing capistrano deploy.) using Capistrano. The cause is when capistrano creates temporary directory for destination (remote) directory and directory for source (local) directory point to same directory. This problem does not happen when you deploy to different machine. Solution Add the below code to your deploy.rb!! Just change the name for temoprary directory used during deployment name. # put this line at the top of app.yml in order to use SecureRandom methods require "securerandom" # set flag true when local deployment set :deploy_to_self, "true" # hook tasks before :deploy, "local:create_dir" after :deploy, "local:clean_dir" #################################### # setup copy_dir, copy_remote_dir # when deploying to machine which this script is running on to avoid copy issue # na

Java: Extract img src from HTML

Here is an code snippet for extracting image src from html. private static final Pattern IMG_SRC_PATTERN = Pattern.compile("<img\\s+.*src\\s*=\\s*('|\")(.+?)\\1.+?>"); public static List<String> extractImgSrces(final String content) { List<String> list = new ArrayList<>(); final Matcher matcher = IMG_SRC_PATTERN.matcher(content); while(matcher.find()){ list.add(matcher.group(2)); } return list; } The example usage is below. In this example, you should only prepare HttpUtils.getStringContentsFromURL method, which is getting html from given url, for your self. public static void main(String[] args) throws URISyntaxException, IOException { extractImgSrces(HttpUtils.getStringContentsFromURL("http://www.google.com/", "utf-8")).stream().forEach(System.out::println); }

Set Specific Revision Number for SVN External

Sometimes, You might want to set revision number for svn-external repository explicitly. In that case, you should just set svn-external with "-rXXXX" on your svn property (XXXX is the revision number you want to set)! I confimred it worked perfectly on windows, linux svn client. lib -rXXXX [svn-external target url]

Get Original Hi-res image from Zoomable image on Amazon

Some pages in amazon provide images which user can zoom on, like this page. The question is "Where is the original hi-resolution image?". You can easily find the explanation page for how to construct url for original hi-res image. (See this stack overflow page .) In this post I will show you the code snippet for getting original hires imgae from ASIN code. Note!: This method might be unavailable if amazon change the implementation of zoom and hires image url construnction. Code Actually code is nothing special. Just find " DynAPI.addZoomViewer( " . // pattern for extracting image code used for retrieving original image. private static Pattern AMAZON_ZOOM_IMAGE_PATTERN = Pattern.compile("DynAPI\\.addZoomViewer\\(\".+/(.+?)\",\\s*(.+?),\\s*(.+?),\\s*(.+?),\\s*(.+?),\\s*(.+?),.*\\)"); public static String findOriginalZoomImageUrl(String asin, String country) { String url = getAmazonZoomImageWindowUrl(asin, country); try{

Build Get Parameter String from Javascript Object or Map

This is just a quick tips... I think you can easily understand whole thing from the code below. // example data used for get parameters. You can use plain javascript object if you want. var map = ["name1":"value1", "name2":"value2"]; // Ok, let’s build string... var parameters = []; for(var prop in map) { // if the name part (prop) contains the characters to uri encode, // use encodeURIComponent as well for the name part. parameters.push(prop + "=" + encodeURIComponent(map[prop])); } var parametersStr = parameters.join("&");

How to pick up Hi-Resolution Image from Amazon product page

Recently I realized that Amazon offers high resolution photos in some products. I have found the way to extract these hires images. Note Note!: This method might be unavailable in the future because Amazon may not like this kind of hack or change the implementation :P Basic Strategy For example, this page: http://www.amazon.com/Silver-Violin-Nicola-Benedetti/dp/B008CYV046/ If you see the html source of this page, you can find the following json string. var colorImages = {"initial":[{"large":"http://ecx.images-amazon.com/images/I/XXXXXXX.jpg",.... It represents the urls of various size of images. If you would like to see the hi-resolution image, you should pick up the url which is defined in hiRes property. Source Code You know I still like Java, I will show you the simple Java code. I use Jackson for parsing JSon string. import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; imp

Java: How to Set Proxy on HttpURLConnection

If you need to set proxy address on Java HttpURLConnection. Do something like below. public static String getString(String urlStr) throws Exception { HttpURLConnection connection = null; InputStream is = null; try { URI uri = new URI(urlStr); // settin proxy String proxyHost = ""; // your proxy serever address int proxyPort = 8080; // your proxy port Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); // open connection with passing Proxy Object connection = (HttpURLConnection)uri.toURL().openConnection(proxy); connection.connect(); is = connection.getInputStream(); String content = toString(is); return content; } finally{ closeQuietly(is); if(connection != null) { connection.disconnect(); } } } private static String toString(InputStream is) throws IOException { byt

Restrict Html Tags which User Can Input (PHP)

I tried to implement very simple html edit text area which has available tags user can input are restricted. So I needed to implement a validator which detects tags not allowed to use. The proper (but a bit heavy) implementation approach is using Tidy . It can validate entire html and also fix and clean up html source! However in my case using tidy is a bit overkill solution. Instead of using tidy, I decided to use strip_tags function. The disadvantage is that the function does not validate html syntax. e.g. inaccurate than using tidy.- "strip_tags function does not actually validate the html, partial or broken tags can result in the removal of more text/data than expected." as the official PHP document says. Okie, as long as we understand the disadvantage, we can use this function. Let's show you the code. function validateOnlyAllowedTags($html, $tags) { $stripped = strip_tags($html, $tags); // if no tags are stripped, the length of html contents

Creating SVN Tag in Bash

In this post, I will show you an example bash script for creating svn tag. First, if you would like to create tag on svn, you simply execute the following command. svn copy 'source_url' 'tag_url' Okay then, we simply call the above command in bash script. I use revision number and time stamp as tag name. Instead of using revision number, maybe you can prepare version number file and count up it when creating tag. #!/bin/bash SVN_USERNAME=dukesoftware SVN_PASSWORD=some_password SVN_INFO_COMMAND="svn info --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive" SVN_COPY_COMMAND="svn copy --username $SVN_USERNAME --password $SVN_PASSWORD --non-interactive" svn_project_trunk="http://somewhere.svn/project/trunk" svn_project_base=${svn_project_trunk/trunk/} # Getting revision from svn info command # Need this line for displaying message of svn info command in English forcibly. export LC_MESSAGES=C revision=`$SVN_INFO_CO

Html Layout by JSP

Introduction In this post, I will introduce how to achieve html layout by jsp. You just define layout and put piece of elements in each actual jsp. I won't explain details but just show you an quick example for sharing "header & footer" in all jsp pages. If you are interested in how & why it works, please refer jsp documentation (or googling). Code Simply you need to prepare a layout tag file and actual jsp files. Prepare layout.tag (actually name is not so important) and put it under WEB-INF/tags. Put header.jsp and footer.jsp under WEB-INF/views/ Create actual jsp which include taglib you previously defined above step. Ok now I will show you the actual codes. layout.tag A key part is using fragment feature. <%@tag description="Layout template" pageEncoding="UTF-8"%> <%@attribute name="main" fragment="true" %> <%@attribute name="head" fragment="true" %> <%@attribu

URI Encode for Java

I always forget and struggling about the behavior of Java methods and classes for encoding uri. To clear my heads I will post small test codes. import java.net.URI; import java.net.URLEncoder; import java.util.BitSet; import org.apache.commons.httpclient.util.URIUtil; public class URIEncodeExample { // hehe using Japanese :p private static final String INPUT = "あいうえお/"; public static void main(String[] args) throws Exception { // java.net.URI // slash is not encoded because it is detected as a path separator // %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/ System.out.println(URI.create(INPUT).toASCIIString()); // java.net.URI + relativize // slash is not encoded because it is detected as a path separator // %E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A/ URI root = new URI("http", "dummy.com", null, null); URI uri = new URI("http", "dummy.com",

Regex: Detect Repeated String Chunks

Target strings I am trying to detect string which contains repeated string pattern like below. .....ABCDABCDABCD..... "ABCD" is just an example, it can be any fixed length of string chunk. Solution Regex Thinking for a bit and finally reached the solution regex which meets my demand is something like below. (.{4,})\\1{3,} The above regex matches string whose length of the chunk is more than 4 and it should be repeated equal or more than 4 times. In more general regex is below (using pattern formatting). String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1) Java Code Ok you know I am Java lover, I will show you full regex code with test cases. If you found any bugs on the code please feel free to comment. public static final boolean isRepeatedStrIn(String input, int minChunkLen, int times) { return input.matches(String.format("(.{%d,})\\1{%d,}", minChunkLen, times-1)); } package com.dukesoftware.utils.common; import java.util.Array