franta-hg@15: /**
franta-hg@15: * Free Telco Dictionary
franta-hg@15: * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@15: *
franta-hg@15: * This program is free software: you can redistribute it and/or modify
franta-hg@15: * it under the terms of the GNU General Public License as published by
franta-hg@15: * the Free Software Foundation, either version 3 of the License, or
franta-hg@15: * (at your option) any later version.
franta-hg@15: *
franta-hg@15: * This program is distributed in the hope that it will be useful,
franta-hg@15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@15: * GNU General Public License for more details.
franta-hg@15: *
franta-hg@15: * You should have received a copy of the GNU General Public License
franta-hg@15: * along with this program. If not, see .
franta-hg@15: */
franta-hg@15: package cz.frantovo.telco.dictionary;
franta-hg@15:
franta-hg@17: import java.io.DataOutputStream;
franta-hg@17: import java.io.IOException;
franta-hg@17: import java.nio.charset.StandardCharsets;
franta-hg@17: import java.util.Objects;
franta-hg@17:
franta-hg@15: /**
franta-hg@130: * Represents one item in StarDict index file (.idx)
franta-hg@130: * which links the term and its position and length in data file (.dict)
franta-hg@130: *
franta-hg@15: * @author Ing. František Kučera (frantovo.cz)
franta-hg@15: */
franta-hg@15: public class IndexEntry implements Comparable {
franta-hg@15:
franta-hg@17: private String name;
franta-hg@17: private long offset;
franta-hg@17: private long length;
franta-hg@17: private long ordinal;
franta-hg@17:
franta-hg@17: public IndexEntry(String name, long offset, long length) {
franta-hg@17: this.name = name;
franta-hg@17: this.offset = offset;
franta-hg@17: this.length = length;
franta-hg@17: }
franta-hg@17:
franta-hg@17: public void serialize(DataOutputStream indexOutputStream) throws IOException {
franta-hg@17: indexOutputStream.write(name.getBytes(StandardCharsets.UTF_8));
franta-hg@17: indexOutputStream.write(0);
franta-hg@17: indexOutputStream.writeInt((int) offset); // unsigned int 32
franta-hg@17: indexOutputStream.writeInt((int) length); // unsigned int 32
franta-hg@17: }
franta-hg@17:
franta-hg@17: public void setOrdinal(long ordinal) {
franta-hg@17: this.ordinal = ordinal;
franta-hg@17: }
franta-hg@17:
franta-hg@17: public long getOrdinal() {
franta-hg@17: return ordinal;
franta-hg@17: }
franta-hg@17:
franta-hg@15: @Override
franta-hg@15: public int compareTo(IndexEntry o) {
franta-hg@17: int nameDiff = name.compareTo(o.name);
franta-hg@17: if (nameDiff == 0) {
franta-hg@17: int offsetDiff = ((Long) offset).compareTo(o.offset);
franta-hg@17: if (offsetDiff == 0) {
franta-hg@17: return ((Long) length).compareTo(o.length);
franta-hg@17: } else {
franta-hg@17: return offsetDiff;
franta-hg@17: }
franta-hg@17: } else {
franta-hg@17: return nameDiff;
franta-hg@17: }
franta-hg@17: }
franta-hg@17:
franta-hg@17: @Override
franta-hg@17: public boolean equals(Object o) {
franta-hg@17: return o instanceof IndexEntry && compareTo((IndexEntry) o) == 0;
franta-hg@17: }
franta-hg@17:
franta-hg@17: @Override
franta-hg@17: public int hashCode() {
franta-hg@17: int hash = 5;
franta-hg@17: hash = 53 * hash + Objects.hashCode(this.name);
franta-hg@17: hash = 53 * hash + (int) (this.offset ^ (this.offset >>> 32));
franta-hg@17: hash = 53 * hash + (int) (this.length ^ (this.length >>> 32));
franta-hg@17: return hash;
franta-hg@15: }
franta-hg@15: }