Skip to content

List型を引数に取るコンストラクタを利用した際にlistの操作がPatternsに影響する #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lambig opened this issue Aug 18, 2023 · 0 comments
Labels
good first issue Good for newcomers

Comments

@lambig
Copy link
Owner

lambig commented Aug 18, 2023

改修について

現状

Pattensはlistを受け取った際にそのlistオブジェクトをそのままインスタンスに保持する。
そのため、受け取ったlistがmutableな場合には以下の操作で定義されたPatternsの挙動が改変できてしまう。

破壊例

            var list = new ArrayList<>(
                    List.<Tuple2<Predicate<Integer>, Function<Integer, String>>>of(
                            when(equalsTo(3), then("b")), // このパターンが消されてしまうので
                            when(equalsTo(4), thenSupply(() -> "c")),
                            when(i -> i > 0, thenApply(Object::toString)), // 3はこちらで処理されるようになる
                            when(i -> i < 0, thenApply(sequenceOf((UnaryOperator<Integer>) i -> i + 1, Number::longValue, Object::toString)))));
            Patterns<Integer, String> target = Patterns.of(list);
            list.remove(0);

対応

これを防ぐため、Patternsは受け取ったリストのコピーを保持するように改修する必要がある。

単体テスト

実際にPatternsを破壊するテストケースが実装されているので、
そのケースによってPatternsが破壊されなくなることを確認したらそのケース名のみを変更すればよい。

@lambig lambig added the good first issue Good for newcomers label Aug 18, 2023
lambig added a commit that referenced this issue Oct 16, 2023
@lambig lambig closed this as completed Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant