2 * Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
4 package com
.intellij
.patterns
;
6 import com
.intellij
.patterns
.MatchingContext
;
7 import com
.intellij
.patterns
.NullablePatternCondition
;
8 import com
.intellij
.patterns
.PatternCondition
;
9 import com
.intellij
.patterns
.TraverseContext
;
10 import org
.jetbrains
.annotations
.NotNull
;
11 import org
.jetbrains
.annotations
.Nullable
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
20 public class CollectionPattern
<T
> extends ObjectPattern
<Collection
<T
>, CollectionPattern
<T
>> {
21 protected CollectionPattern() {
22 super(new NullablePatternCondition() {
23 public boolean accepts(@Nullable final Object o
,
24 final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
25 return o
instanceof Collection
;
30 public CollectionPattern
<T
> all(final ElementPattern pattern
) {
31 return with(new PatternCondition
<Collection
<T
>>() {
32 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
33 for (final T t
: collection
) {
34 if (!pattern
.accepts(t
, matchingContext
, traverseContext
)) return false;
41 public CollectionPattern
<T
> atLeastOne(final ElementPattern pattern
) {
42 return with(new PatternCondition
<Collection
<T
>>() {
43 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
44 for (final T t
: collection
) {
45 if (pattern
.accepts(t
, matchingContext
, traverseContext
)) return true;
52 public CollectionPattern
<T
> filter(final ElementPattern elementPattern
, final ElementPattern continuationPattern
) {
53 return with(new PatternCondition
<Collection
<T
>>() {
54 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
55 List
<T
> filtered
= new ArrayList
<T
>();
56 for (final T t
: collection
) {
57 if (elementPattern
.accepts(t
, matchingContext
, traverseContext
)) {
61 return continuationPattern
.accepts(filtered
, matchingContext
, traverseContext
);
66 public CollectionPattern
<T
> first(final ElementPattern elementPattern
) {
67 return with(new PatternCondition
<Collection
<T
>>() {
68 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
69 return !collection
.isEmpty() && elementPattern
.accepts(collection
.iterator().next(), matchingContext
,
75 public CollectionPattern
<T
> empty() {
79 public CollectionPattern
<T
> notEmpty() {
83 public CollectionPattern
<T
> atLeast(final int size
) {
84 return with(new PatternCondition
<Collection
<T
>>() {
85 public boolean accepts(@NotNull final Collection
<T
> ts
,
86 final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
87 return ts
.size() >= size
;
92 public CollectionPattern
<T
> size(final int size
) {
93 return with(new PatternCondition
<Collection
<T
>>() {
94 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
95 return size
== collection
.size();
100 public CollectionPattern
<T
> last(final ElementPattern elementPattern
) {
101 return with(new PatternCondition
<Collection
<T
>>() {
102 public boolean accepts(@NotNull final Collection
<T
> collection
, final MatchingContext matchingContext
, @NotNull final TraverseContext traverseContext
) {
103 if (collection
.isEmpty()) {
107 for (final T t
: collection
) {
110 return elementPattern
.accepts(last
, matchingContext
, traverseContext
);