PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/testability-explorer/src/main/java/com/google/test/metric/method/Block.java

http://testability-explorer.googlecode.com/
Java | 101 lines | 71 code | 15 blank | 15 comment | 9 complexity | b9fe6d3208726792e5e5ed4de56c5f82 MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.method;
 17
 18import static java.util.Collections.unmodifiableList;
 19
 20import java.util.ArrayList;
 21import java.util.List;
 22
 23import com.google.test.metric.method.op.stack.Load;
 24import com.google.test.metric.method.op.stack.StackOperation;
 25
 26public class Block {
 27
 28  private final String id;
 29  private final List<Block> previousBlocks = new ArrayList<Block>();
 30  private final List<StackOperation> operations = new ArrayList<StackOperation>();
 31  private final List<Block> nextBlocks = new ArrayList<Block>();
 32  private final boolean isTerminal = false;
 33  private Constant exception;
 34
 35  public Block(String id) {
 36    this.id = id;
 37  }
 38
 39  public void addNextBlock(Block nextBlock) {
 40    if (!nextBlocks.contains(nextBlock) && nextBlock != this) {
 41      nextBlocks.add(nextBlock);
 42      nextBlock.previousBlocks.add(this);
 43    }
 44  }
 45
 46  public void addOp(StackOperation operation) {
 47    operations.add(operation);
 48  }
 49
 50  public List<StackOperation> getOperations() {
 51    return operations;
 52  }
 53
 54  @Override
 55  public String toString() {
 56    StringBuilder buf = new StringBuilder();
 57    buf.append(exception == null ? "Block" : "ExceptionHandlerBlock");
 58    buf.append("[");
 59    buf.append(id);
 60    String sep = " -> ";
 61    for (Block next : nextBlocks) {
 62      buf.append(sep);
 63      buf.append(next.id);
 64      sep = ", ";
 65    }
 66    sep = " <- ";
 67    for (Block next : previousBlocks) {
 68      buf.append(sep);
 69      buf.append(next.id);
 70      sep = ", ";
 71    }
 72    buf.append("]{\n");
 73    for (StackOperation operation : operations) {
 74      buf.append("  ");
 75      buf.append(operation);
 76      buf.append("\n");
 77    }
 78    buf.append("}");
 79    return buf.toString();
 80  }
 81
 82  public List<Block> getNextBlocks() {
 83    return unmodifiableList(nextBlocks);
 84  }
 85
 86  public boolean isTerminal() {
 87    return isTerminal;
 88  }
 89
 90  public String getId() {
 91    return id;
 92  }
 93
 94  public void setExceptionHandler(int lineNumber, Constant exception) {
 95    if (this.exception == null) {
 96      operations.add(0, new Load(lineNumber, exception));
 97    }
 98    this.exception = exception;
 99  }
100
101}