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

投稿

11月, 2014の投稿を表示しています

PHP: Execute Any DB Access Code in Transaction

Reusable Code for Execute Any DB Access Code in Transaction In this post, I will show you highly reusable utility code for wrapping and executing original db access code in transaction. Anyway let's show you the code. function executeInTransaction(callable $func) { $conn = new PDO(SERVER_NAME, USERNAME, PASSWORD, DBNAME); try { $conn->beginTransaction(); $func($conn); $conn->commit(); } catch (Exception $ex) { $conn->rollBack(); throw $ex; } } Oh, how to use? You just simply passing your core DB access code as the callable $func argument. Okay, let me show you example in next section. Example Usage First, assume you have following 2 methods for inserting data to DB. CONST SERVER_NAME = 'localhost'; CONST USERNAME = "username"; CONST PASSWORD = "password"; CONS...

Java: Compute Source Code Similarity Based on Jaccard Similarity Coefficient

Compute Source Code Similarity I have tried to analyze source code by various methods recently. In this post, I will show you my artifact - Compute source code similarity based on Jaccard Similarity Coefficient. Jaccard Similarity Coefficient is an common and quick techniqueto compute similarity between 2 documents. You can see more details in Jaccard index or MinHash . My similarity computation strategy is below: Calculate hash value for each line for each files. Create set which contains hashes calculated in the above process for the each files. Compute Jaccard Similarity Coefficient for all combinations of the files. Source Code Now I can show you code snippets for compute source code similarity based on Jaccard Similarity Coefficient. Jaccard Simlarity package com.dukesoftware.utils.text; import java.util.HashSet; import java.util.Set; /** * Jaccard similarity coefficient http://en.wikipedia.org/wiki/MinHash */ public class JaccardSimlarity<T> { priv...

Java 8: Process Each Line in File

Here is the code for iterating each line in file. You simply write lambda expression for processing line and doing something nice. private static void processLine(File file, Consumer<String> lineProcessor) throws IOException{ try(FileReader in = new FileReader(file); BufferedReader br = new BufferedReader(in)){ String line; while ((line = br.readLine()) != null) { lineProcessor.accept(line); } } } The below code is an example usage of processLine method. The code simply print out lines in file. public static void main(String[] args) throws IOException { processLine(new File("c:/temp/test.txt"), System.out::println); }

Bash Script for Back Up Files to Subversion

Introduction There are 2 ways to control files on multiple machines in the software development world. Deploy or build file from version control system Backup files to version control system In my opinion, ideally, everything should be done by the way 1 because it ensures the files are same across the all machines. However in the real world, sometime it is hard to deploy all files from version control system because of the various reasons - such as linux/unix permission or organization structure or too difficult to automating deployment or etc. etc.... In that case, we should take the way 2. In this post, I will introduce simple bash script for back up files to Subversion. Of course you can use your favorite version control system such as Git instead of Subversion ;) If you are a software developer, you can easily refine my bash script and create git version quickly ;) Bash Script for Back Up Files to Subversion See below script! You just save the script and simply execut...

Java: Remove Comments, Annotations and Extra Spaces From Source Code

I have written Java code for removing comments, annotations and extra spaces from Java source code. package com.dukesoftware.utils.text; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import com.dukesoftware.utils.io.IOUtils; public class JavaSourceUtils { public static void main(String[] args) throws FileNotFoundException, IOException { String cleanedUpSource = removeCommentAndAnnotation(IOUtils.userDirectory( "workspace2/DukeSoftwareUtils/src/main/java/com/dukesoftware/utils/text/JavaSourceUtils.java" )); System.out.println(cleanedUpSource); } public static final String removeCommentAndAnnotation(File file) throws IOException { try(BufferedReader br = new BufferedReader(new FileReader(file))) { return removeCommentAndAnnotation(br); } } private final static int COD...

PHP: Process Each File in Directory

PHP Code function processEachFileIn($path, callable $callback) { $objects = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); foreach ($objects as $object) { if (!$object->isDir() && $object->getFilename() != '.' && $object->getFilename() != '..') { $callback($object); } } } Example Usage The below code demonstrates print all file names in directory /tmp recursively. processEachFileIn('/tmp', function($file){ echo $file->getFilename()."\n"; });

Java: Get Sorted Array Index

As you know, if you would like to sort array, you should use Arrays.sort method. But if you would like to get sorted index, you should use your a brain a bit. In this post, I will show you the code snippet for getting sorted array index. The following is the simplest way to get sorted index. The disadvantages of the following code are autoboxing in compare process and returned array type is not int[] but Integer[]. /** * The most common & general way. But this method use autoboxing and need Integer[] not int[]. */ public static final <T>void sort(T[] array, Comparator<T> comparator, Integer[] sorted) { Arrays.sort(sorted, (i1, i2) -> comparator.compare(array[i1], array[i2])); } To solve these advantage, I wrote following array sort helper class which can get sorted array index as int[] type. You can call getSortedIndex method with source array and comparator as the arguments. The code uses lambda expression which was introduced in Java 8. packa...