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

投稿

Collection内の重複を見つけるJavaコード

Collection内の重複を見つけるJavaコードをGoogleのMultimapを使って書いてみました。 引数のkeyGeneratorによって、重複したとみなす要素の戦略を変更できます。実装にMapを使っていますので、keyGegerator関数で生成されるKeyオブジェクトは、equalsメソッドとhashCodeメソッドが正しく実装されている必要があります。 import java.util.Collection; import java.util.Map.Entry; import com.google.common.base.Function; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; public class DuplicationFinder { public static Multimap findDuplication(Collection c, Function keyGenerator){ return toResult(createMultimap(c, keyGenerator)); } private static Multimap toResult(final Multimap temp) { final Multimap result = ArrayListMultimap.create(); for(Entry > entry : temp.asMap().entrySet()){ if(entry.getValue().size() > 1){ result.putAll(entry.getKey(), entry.getValue()); } } return result; } private static Multimap createMultimap(Collection c, Function keyGenerator) { final Multimap map = ArrayListMultimap.create(); for(V v : c){ map

C#からOutlookを操作

「メールを受信したときに実行するプログラム」と「メールボックス内のフォルダ情報やinbox内にあるメールを表示する」サンプルコードです。 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Interop.Outlook; namespace OutlookExample { public class OutlookExample { public static void RunOutlookExample() { ApplicationClass appClass = new ApplicationClass(); appClass.NewMail += new ApplicationEvents_10_NewMailEventHandler(outLookApp_NewMailEx); PrintInbox(appClass); } private static void outLookApp_NewMailEx(string EntryIDCollection) { // do something nice when mail is coming } public static void PrintInbox(ApplicationClass o) { // get items in my inbox (using MAPI) NameSpace outlookNS = o.GetNamespace("MAPI"); MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox); foreach (MA

ActionScript 3.0: String Utility Methods (trim, startsWith, endsWith etc.)

public class StringUtils { public function StringUtils() { } public static function concatAsString(array:Array):String { var ret:String = ""; for each(var str:String in array) { ret += str; } return ret; } public static function concatAllAsString(delimita:String, ...strs):String { if (strs == null || strs.length == 0) { return ""; } var ret:String = ""; for each(var str:String in strs) { ret += delimita + str } return ret.substr(1, ret.length - 1); } public static function trim(src:String):String { return src.replace(/^[\s\t]+(.+)[\s\t]+$/, "$1"); } public static function endsWith(str:String, seacrh:String):Boolean { return str.length > seacrh.length && str.substr(seacrh.length-1, seacrh.length) === seacrh; } public static function startsWith(prefix:String, testStr:String):Boolean { return testStr.length >= prefix.length && testStr.substr(0, prefix.len

Javaで全画面表示

Javaで全画面表示する方法です。GraphicsDeviceのsetFullScreenWindow メソッドの引数に全画面表示させたいWindowオブジェクトを渡すことで実現できます。サンプルコードを以下に示します。 public static GraphicsDevice setFullScreen(Window window) {    GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();    graphicsDevice.setFullScreenWindow(window);    return graphicsDevice; // not necessary to return but... easy to reuse GraphicsDevice  object after this method is called } 全画面表示を終了させるときは、GraphicsDeviceにnullをセットします。 GraphicsEnvironment .getLocalGraphicsEnvironment() .getDefaultScreenDevice() .graphicsDevice.setFullScreenWindow(null); 例えばこんな感じで使います。 public class FullScreenTest {     @Test     public void testFullScreen() throws Exception {         JWindow window = createTestWindow();         window.setVisible(true);                 GraphicsDevice graphicsDevice = AwtUtils.setFullScreen(window);         try {             Thread.sleep(1000);         } catch (InterruptedException e) {}

HTML related library for Java

HTML Parser JavaのHTML Parser でいまだにしっくりくるライブラリを見つけられないのですが、私がいくつか試したものを紹介します。 JTidy 特にXHTML形式のファイルの解析で威力を発揮します。 HTMLEditorKit : Swingに付属しているものです。個人的にはSwingのライブラリをHTMLの解析の目的で使うのはどうかなあと感じています。 NekoHTML 残念ながらまだ試していませんが、これが使いやすそうです。機会があればBlogに書こうと思います。 StackOveflow  の Java HTML Parsing の議論が参考になりそうです。 Htm Parser jsoup   HTML Validator JTidy