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