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

org.sonar.java.ast.parser.ListTreeImpl Maven / Gradle / Ivy

There is a newer version: 3.11
Show newest version
/*
 * SonarQube Java
 * Copyright (C) 2012 SonarSource
 * [email protected]
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.java.ast.parser;

import com.google.common.collect.AbstractIterator;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.sonar.java.model.JavaTree;
import org.sonar.plugins.java.api.tree.ListTree;
import org.sonar.plugins.java.api.tree.SyntaxToken;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TreeVisitor;
import org.sonar.sslr.grammar.GrammarRuleKey;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public abstract class ListTreeImpl extends JavaTree implements ListTree {

  private final List list;
  private final List separators;

  public ListTreeImpl(GrammarRuleKey grammarRuleKey, List list) {
    super(grammarRuleKey);
    this.list = list;
    this.separators = Lists.newArrayList();
  }
  public ListTreeImpl(GrammarRuleKey grammarRuleKey, List list, List separators) {
    super(grammarRuleKey);
    this.list = list;
    this.separators = separators;
  }

  @Override
  public List separators() {
    return separators;
  }


  @Override
  public void accept(TreeVisitor visitor) {
    for (T t : list) {
      ((Tree) t).accept(visitor);
    }
  }

  @Override
  public Kind kind() {
    return Kind.LIST;
  }

  @Override
  public Iterator childrenIterator() {
    return new InterleaveIterator<>(ImmutableList.of(((Iterable) list).iterator(), separators.iterator()));
  }
  private static class InterleaveIterator extends AbstractIterator{

    private final LinkedList> iterables;

    public InterleaveIterator(List> iterables) {
      super();
      this.iterables = new LinkedList<>(iterables);
    }

    @Override
    protected E computeNext() {
      while(!iterables.isEmpty()) {
        Iterator topIter = iterables.poll();
        if(topIter.hasNext()) {
          E result = topIter.next();
          iterables.offer(topIter);
          return result;
        }
      }
      return endOfData();
    }
  }
  @Override
  public int size() {
    return list.size();
  }

  @Override
  public boolean isEmpty() {
    return list.isEmpty();
  }

  @Override
  public boolean contains(Object o) {
    return list.contains(o);
  }

  @Override
  public Iterator iterator() {
    return list.iterator();
  }

  @Override
  public Object[] toArray() {
    return list.toArray();
  }

  @Override
  public  T[] toArray(T[] a) {
    return list.toArray(a);
  }

  @Override
  public boolean add(T e) {
    return list.add(e);
  }

  @Override
  public boolean remove(Object o) {
    return list.remove(o);
  }

  @Override
  public boolean containsAll(Collection c) {
    return list.containsAll(c);
  }

  @Override
  public boolean addAll(Collection c) {
    return list.addAll(c);
  }

  @Override
  public boolean addAll(int index, Collection c) {
    return list.addAll(index, c);
  }

  @Override
  public boolean removeAll(Collection c) {
    return list.removeAll(c);
  }

  @Override
  public boolean retainAll(Collection c) {
    return list.retainAll(c);
  }

  @Override
  public void clear() {
    list.clear();
  }

  @Override
  public T get(int index) {
    return list.get(index);
  }

  @Override
  public T set(int index, T element) {
    return list.set(index, element);
  }

  @Override
  public void add(int index, T element) {
    list.add(index, element);
  }

  @Override
  public T remove(int index) {
    return list.remove(index);
  }

  @Override
  public int indexOf(Object o) {
    return list.indexOf(o);
  }

  @Override
  public int lastIndexOf(Object o) {
    return list.lastIndexOf(o);
  }

  @Override
  public ListIterator listIterator() {
    return list.listIterator();
  }

  @Override
  public ListIterator listIterator(int index) {
    return list.listIterator(index);
  }

  @Override
  public List subList(int fromIndex, int toIndex) {
    return list.subList(fromIndex, toIndex);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy