Definitions.java revision 415
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
457N/A
0N/A/*
0N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/A
0N/Apackage org.opensolaris.opengrok.analysis;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.Serializable;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.util.Set;
0N/A
0N/Apublic class Definitions implements Serializable {
0N/A /** Map from symbol to the line numbers on which the symbol is defined. */
0N/A private final Map<String, Set<Integer>> symbols;
0N/A /** List of all the tags. */
456N/A private final List<Tag> tags;
0N/A
0N/A Definitions() {
0N/A symbols = new HashMap<String, Set<Integer>>();
0N/A tags = new ArrayList<Tag>();
0N/A }
0N/A
0N/A /**
0N/A * Get all symbols used in definitions.
0N/A * @return a set containing all the symbols
0N/A */
0N/A public Set<String> getSymbols() {
0N/A return symbols.keySet();
0N/A }
0N/A
0N/A /**
0N/A * Check if there is a tag for a symbol.
0N/A * @param symbol the symbol to check
0N/A * @return {@code true} iff there is a tag for {@code symbol}
0N/A */
0N/A public boolean hasSymbol(String symbol) {
0N/A return symbols.containsKey(symbol);
0N/A }
0N/A
0N/A /**
0N/A * Check whether the specified symbol is defined on the given line.
0N/A * @param symbol the symbol to look for
0N/A * @param lineNumber the line to check
0N/A * @return {@code true} iff {@code symbol} is defined on the specified line
0N/A */
0N/A public boolean hasDefinitionAt(String symbol, int lineNumber) {
0N/A Set<Integer> lines = symbols.get(symbol);
0N/A return lines != null && lines.contains(lineNumber);
0N/A }
0N/A
0N/A /**
0N/A * Return the number of occurrences of definitions with the specified
0N/A * symbol.
0N/A * @param symbol the symbol to count the occurrences of
0N/A * @return the number of times the specified symbol is defined
0N/A */
0N/A public int occurrences(String symbol) {
457N/A Set<Integer> lines = symbols.get(symbol);
0N/A return lines == null ? 0 : lines.size();
0N/A }
0N/A
0N/A /**
0N/A * Return the number of distinct symbols.
0N/A * @return number of distinct symbols
0N/A */
0N/A public int numberOfSymbols() {
0N/A return symbols.size();
0N/A }
0N/A
0N/A /**
0N/A * Get a list of all tags.
0N/A * @return all tags
0N/A */
0N/A public List<Tag> getTags() {
0N/A return tags;
0N/A }
0N/A
0N/A /**
0N/A * Class that represents a single tag.
0N/A */
0N/A public static class Tag implements Serializable {
/** Line number of the tag. */
public final int line;
/** The symbol used in the definition. */
public final String symbol;
/** The type of the tag. */
public final String type;
/** The full line on which the definition occurs. */
public final String text;
private Tag(int line, String symbol, String type, String text) {
this.line = line;
this.symbol = symbol;
this.type = type;
this.text = text;
}
}
void addTag(int line, String symbol, String type, String text) {
// The strings are frequently repeated (a symbol can be used in
// multiple definitions, multiple definitions can have the same type,
// one line can contain multiple definitions). Intern them to minimize
// the space consumed by them (see bug #809).
final String internedSymbol = symbol.intern();
final String internedType = type.intern();
final String internedText = text.intern();
tags.add(new Tag(line, internedSymbol, internedType, internedText));
Set<Integer> lines = symbols.get(internedSymbol);
if (lines == null) {
lines = new HashSet<Integer>();
symbols.put(internedSymbol, lines);
}
lines.add(line);
}
/**
* Create a binary representation of this object.
* @return a byte array representing this object
* @throws IOException if an error happens when writing to the array
*/
public byte[] serialize() throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
new ObjectOutputStream(bytes).writeObject(this);
return bytes.toByteArray();
}
/**
* Deserialize a binary representation of a {@code Definitions} object.
* @param bytes a byte array containing the {@code Definitions} object
* @return a {@code Definitions} object
* @throws IOException if an I/O error happens when reading the array
* @throws ClassNotFoundException if the class definition for an object
* stored in the byte array cannot be found
* @throws ClassCastException if the array contains an object of another
* type than {@code Definitions}
*/
public static Definitions deserialize(byte[] bytes)
throws IOException, ClassNotFoundException {
ObjectInputStream in =
new ObjectInputStream(new ByteArrayInputStream(bytes));
return (Definitions) in.readObject();
}
}