LoginSignup
3
4

More than 5 years have passed since last update.

[Javaの小枝] 何かの値を一時的に変更したいけど戻すの忘れたくない

Posted at

何かの値を一時的に変更したいけど戻すの忘れそう!って時に、こんなコードを書いて使っている。何かもっと良いアイデアをお持ちの方もいるかもしれない。あったら教えて欲しい。

Sample.java
    public static class SelfRestorable<T> implements AutoCloseable {
        public static <T> SelfRestorable<T> restoreTo(T value) {return new SelfRestorable<T>(value);}

        private final T originalValue;
        private Consumer<T> setter = null;
        public SelfRestorable(T value) {originalValue = value;}
        public SelfRestorable<T> by(Consumer<T> setter_) {setter = setter_; return this;}
        @Override public void close() throws Exception {if (setter != null) setter.accept(originalValue);}
    }

    // 例えばユニットテスト中に一時的に static 変数で定義されている DB の接続先を変更する、など。
    @Test public void test() throws Exception {
        try (
                SelfRestorable<Connection> _s = restoreTo(ConnectionProvider.defaultConnection).by(c -> ConnectionProvider.defaultConnection = c);
        ) {
            // 接続先を必要なところに変える。戻すのは AutoCloseable な SelfRestorable というオブジェクトが行なう
            ConnectionProvider.defaultConnection = temporalConnection;
            // 以下テストコード
        }
    }

ちなみに Java7 と Java8 の AutoCloseable の規約は若干異なっており、以下のように Java7 では解放すべきリソースである、と既定されているが Java8 の元では必ずしもリソースである必要がない。すなわち、Java7の元では以上のコードは厳密には規約違反と言えるのかもしれない。

image2016-7-26 13-55-57.png

Java7 では以上のように記述されていたものが Java8 では以下のようになっている。

image2016-7-26 13-57-17.png

3
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4