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

com.google.gxp.compiler.base..svn.text-base.AbstractRoot.svn-base Maven / Gradle / Ivy

Go to download

Google XML Pages (GXP) is a templating system used to generate XML/SGML markup (most often HTML).

The newest version!
/*
 * Copyright (C) 2008 Google Inc.
 *
 * 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.google.gxp.compiler.base;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.gxp.compiler.alerts.SourcePosition;
import com.google.gxp.compiler.schema.Schema;

import java.util.*;

/**
 * An abstract implementation of {@code Root}.
 */
public abstract class AbstractRoot extends AbstractNode implements Root {
  private final TemplateName.FullyQualified name;
  private final Schema schema;
  private final ImmutableList javaAnnotations;
  private final ImmutableList parameters;
  private final ImmutableList imports;
  private final ImmutableList throwsDeclarations;
  private final ImmutableList formalTypeParameters;

  protected AbstractRoot(SourcePosition pos,
                         String displayName,
                         TemplateName.FullyQualified name,
                         Schema schema,
                         List javaAnnotations,
                         List imports,
                         List throwsDeclarations,
                         List parameters,
                         List formalTypeParameters) {
    super(pos, displayName);
    this.name = Preconditions.checkNotNull(name);
    this.schema = Preconditions.checkNotNull(schema);
    this.javaAnnotations = ImmutableList.copyOf(javaAnnotations);
    this.imports = ImmutableList.copyOf(imports);
    this.throwsDeclarations = ImmutableList.copyOf(throwsDeclarations);
    this.parameters = ImmutableList.copyOf(parameters);
    this.formalTypeParameters = ImmutableList.copyOf(formalTypeParameters);
  }

  protected abstract T self();

  protected abstract T withParameters(List newParameters);

  public T transformParameters(Function parameterTransformer) {
    List newParameters = Util.map(getParameters(), parameterTransformer);

    return Iterables.elementsEqual(getParameters(), newParameters)
        ? self()
        : withParameters(newParameters);
  }

  public TemplateName.FullyQualified getName() {
    return name;
  }

  public Schema getSchema() {
    return schema;
  }

  protected List getJavaAnnotations() {
    return javaAnnotations;
  }

  /**
   * @return all {@link JavaAnnotation}s that are annotating a specific item.
   */
  public Iterable getJavaAnnotations(final JavaAnnotation.Element element) {
    return Iterables.filter(javaAnnotations, new Predicate() {
      public boolean apply(JavaAnnotation javaAnnotation) {
        return Objects.equal(javaAnnotation.getElement(), element);
      }
    });
  }

  public List getImports() {
    return imports;
  }

  public List getThrowsDeclarations() {
    return throwsDeclarations;
  }

  public List getParameters() {
    return parameters;
  }

  public Parameter getParameterByPrimary(String paramName) {
    for (Parameter parameter : getParameters()) {
      if (paramName.equals(parameter.getPrimaryName())) {
        return parameter;
      }
    }
    return null;
  }

  public List getFormalTypeParameters() {
    return formalTypeParameters;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy