SCRAP

PermutationKeyMap

ArrayListの要素をソートすれば、CombinationKeyMapになるかも。 import java.util.*;public class PermutationKeyMap { private Map body = new HashMap(); public int size() { return body.size(); } public boolean isEmpty() { return body.isEmpty(); …

再帰的なファイルの探索

import java.io.*; import java.util.*;public class MkLst { private static final String ROOT = "/foo/bar/hoge"; public static void main(String args) { List flst = new ArrayList(); lsr(new File(ROOT), flst); for (int i = 0; i System.out.print…

sprintfっぽい関数

こんな感じ。 public static String sprintf(String format, Object params) { MessageFormat mformat = new MessageFormat(format); return mformat.format(params); } public static void main(String args) { System.out.println(sprintf("私の名前は{0}…

JavaBeanをHashMap#toString()っぽく表示

いちいちBeanUtilsを使いたくないので。 Beanの親クラスのtoString()に仕込んでおくと便利かも。 private String bean2string(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException, InvocationTargetException …

参照整合性制約を無効化する。

個人的には、単体テスト時は参照整合性制約をはずしておいたほうが便利だと思う。 declare cursor c_constraint is select UC.TABLE_NAME, UC.CONSTRAINT_NAME from USER_CONSTRAINTS UC where UC.OWNER='HOGE' and UC.CONSTRAINT_TYPE='R'; c_sql number; b…

カーソルのスクラップ。

カーソル。 set serveroutput on;declare cursor c_sess is select V_SESS.SADDR from V$SESSION V_SESS; r_sess c_sess%rowtype; begin open c_sess; loop fetch c_sess into r_sess; exit when c_sess%notfound; DBMS_OUTPUT.PUT_LINE(r_sess.saddr); end …

hello, PL/SQL

とりあえず。 set serveroutput on;begin DBMS_OUTPUT.PUT_LINE('hello, PL/SQL'); end; / hello, PL/SQLfor文。 set serveroutput on;declare cnt number; now date; begin for cnt in 1..10 loop select sysdate into now from dual; DBMS_OUTPUT.PUT_LINE…

制御構造のスクラップ

while文。 set serveroutput on;declare cnt number; now date; begin cnt := 0; while cnt select sysdate into now from dual; DBMS_OUTPUT.PUT_LINE(cnt || ': ' || now); cnt := cnt + 1; end loop; end; / 0: 2005-01-20 12:47:48 1: 2005-01-20 12:47:…

呼び出しもとのクラス名、メソッド名を取得する

StackTraceElement[] elems = (new Exception()).getStackTrace(); elems[?].getClassName(); elems[?].getMethodName();…スマートじゃないなぁ。

formで開いた子ウィンドウを親ウィンドウから閉じる。

<html> <head> <title>テスト</title> <script> var child = null; function closeChild() { if(child && !child.closed) { child.close(); } } </script> </head> <body> <form method="post" action="http://hoge/foo/bae" target="foo" onsubmit="child = window.open('', this.target)"> </form></body></html>

Proxyを使った動的なBeanの実装

Beanを生成するクラスが次の通り。 import java.lang.reflect.Proxy;public class DynaBean { private Class clazz = null; public DynaBean(Class clazz) { this.clazz = clazz; } public Object newInstance() { return Proxy.newProxyInstance(clazz.getC…

DBUnitのbase64エンコーダ(for 2.0)

DBUnitのテストデータで、ROW型のデータを記述するときに使用。(なんでbase64なんだ?) import org.dbunit.util.Base64;public class Base64Encoder { public static void main(String args) { if (args.length == 1) { byte src = asByteArray(args[0]); St…

assertEqualsWithRegex()

たま〜に書式しか検証できないことがあるので。 protected void assertEqualsWithRegex(String expectedRegex, String actual) { Perl5Util perl = new Perl5Util(); boolean isMatch = perl.match(expectedRegex, actual); if (!isMatch) { fail("expected:…

日付の演算

以下のような感じ。 package sample.usual;import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;public class DateUtils { public static void main(String[] args) { System.out.print…

submap()

Mapから、指定したプリフィックスのキーと値を取得する。 プロパティファイルでマッピングの定義をするときとかに使う。 private static Map submap(Map map, String prefix, boolean hasPrefix) { Map submap = new HashMap(); Iterator keys = map.keySet(…

クラスパスから設定ファイル読み込み

どんなアプリでもたいてい使うので。 staticな場合は「this→クラス名.class」。 private Properties load(String filename) throws IOException { Properties props = new Properties(); InputStream in = null; try { ClassLoader cloader = Thread.current…

Javaの暗号化

JCE、またはJDK1.4の暗号化ライブラリを使った暗号化のコード。 Crypt 暗号化ユーティリティクラス。 package hoge;import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; i…

join() または implode()

配列にjoin()メソッドがあるといいな…と思ったので。*1 Java版join() (配列) public class Hoge { public static void main(String args) throws Exception { String array1 = {"aaa", "bbb", "ccc"}; String array2 = {"111", null, "333", ""}; String arr…

16進数文字列(String)⇔バイト配列(byte[])

※2013/01/08注 なんか未だに参照されるようなので。。。 DatatypeConverterを使ってみてはどうでしょう? - わりとよく使うので。 public class HexUtil { /** * バイト配列を16進数の文字列に変換する。 * * @param bytes バイト配列 * @return 16進数の文…