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

com.google.gxp.compiler.base..svn.text-base.Concatenation.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.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gxp.compiler.alerts.SourcePosition;
import com.google.gxp.compiler.schema.Schema;

import java.util.*;

/**
 * An {@code Expression} which concatenates other {@code Expression}s together.
 */
public class Concatenation extends Expression {
  private final ImmutableList values;

  private Concatenation(Node fromNode, Schema schema,
                        List values) {
    super(fromNode, schema);
    this.values = ImmutableList.copyOf(values);
    if (values.size() < 2) {
      throw new IllegalArgumentException(
          "Concatenation cannot have only " + values.size() + " values.");
    }
  }

  /**
   * @param sourcePosition sourcePosition of the result, used only when
   * concatentation turns out to be empty. (otherwise the values will be used
   * to compute this)
   * @param values values to concatenate.
   * @return concatenated Expression. This may be a Concatenation, though it
   * some cases constant folding will result in it being a different type of
   * Expression.
   */
  public static Expression create(SourcePosition sourcePosition,
                                  Schema schema,
                                  List values) {
    // TODO(laurence): add more unit tests for this
    // TODO(laurence): add some better consistency checks for concatenating
    // nodes of inconsistent type. This is necessary for content flattener, as
    // it has to deal with cases like "foo