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