All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.patterns.CollectionPattern Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition core-api library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.patterns;

import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author peter
 */
public class CollectionPattern extends ObjectPattern, CollectionPattern> {
  private static final InitialPatternCondition CONDITION = new InitialPatternCondition(Collection.class) {
    public boolean accepts(@Nullable final Object o, final ProcessingContext context) {
      return o instanceof Collection;
    }
  };

  protected CollectionPattern() {
    super(CONDITION);
  }

  public CollectionPattern all(final ElementPattern pattern) {
    return with(new PatternCondition>("all") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        for (final T t : collection) {
          if (!pattern.accepts(t, context)) return false;
        }
        return true;
      }
    });
  }

  public CollectionPattern atLeastOne(final ElementPattern pattern) {
    return with(new PatternCondition>("atLeastOne") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        for (final T t : collection) {
          if (pattern.accepts(t, context)) return true;
        }
        return false;
      }
    });
  }

  public CollectionPattern filter(final ElementPattern elementPattern, final ElementPattern> continuationPattern) {
    return with(new PatternCondition>("filter") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        List filtered = new ArrayList();
        for (final T t : collection) {
          if (elementPattern.accepts(t, context)) {
            filtered.add(t);
          }
        }
        return continuationPattern.accepts(filtered, context);
      }
    });
  }

  public CollectionPattern first(final ElementPattern elementPattern) {
    return with(new PatternCondition>("first") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        return !collection.isEmpty() &&
               elementPattern.accepts(collection.iterator().next(), context);
      }
    });
  }

  public CollectionPattern empty() {
    return size(0);
  }

  public CollectionPattern notEmpty() {
    return atLeast(1);
  }

  public CollectionPattern atLeast(final int size) {
    return with(new PatternCondition>("atLeast") {
      public boolean accepts(@NotNull final Collection ts, final ProcessingContext context) {
        return ts.size() >= size;
      }
    });
  }

  public CollectionPattern size(final int size) {
    return with(new PatternCondition>("size") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        return size == collection.size();
      }
    });
  }

  public CollectionPattern last(final ElementPattern elementPattern) {
    return with(new PatternCondition>("last") {
      public boolean accepts(@NotNull final Collection collection, final ProcessingContext context) {
        if (collection.isEmpty()) {
          return false;
        }
        T last = null;
        for (final T t : collection) {
          last = t;
        }
        return elementPattern.accepts(last, context);
      }
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy