java/dictionary-generator/src/cz/frantovo/telco/dictionary/IndexEntry.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 19 Aug 2013 21:11:47 +0200
changeset 132 42449cdde35c
parent 130 b8dc8a6f82cf
child 151 a9f1ba451247
permissions -rw-r--r--
data: CIMD
franta-hg@15
     1
/**
franta-hg@15
     2
 * Free Telco Dictionary
franta-hg@15
     3
 * Copyright © 2013 František Kučera (frantovo.cz)
franta-hg@15
     4
 *
franta-hg@15
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@15
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@15
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@15
     8
 * (at your option) any later version.
franta-hg@15
     9
 *
franta-hg@15
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@15
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@15
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@15
    13
 * GNU General Public License for more details.
franta-hg@15
    14
 *
franta-hg@15
    15
 * You should have received a copy of the GNU General Public License
franta-hg@15
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@15
    17
 */
franta-hg@15
    18
package cz.frantovo.telco.dictionary;
franta-hg@15
    19
franta-hg@17
    20
import java.io.DataOutputStream;
franta-hg@17
    21
import java.io.IOException;
franta-hg@17
    22
import java.nio.charset.StandardCharsets;
franta-hg@17
    23
import java.util.Objects;
franta-hg@17
    24
franta-hg@15
    25
/**
franta-hg@130
    26
 * Represents one item in StarDict index file (.idx)
franta-hg@130
    27
 * which links the term and its position and length in data file (.dict)
franta-hg@130
    28
 * 
franta-hg@15
    29
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@15
    30
 */
franta-hg@15
    31
public class IndexEntry implements Comparable<IndexEntry> {
franta-hg@15
    32
franta-hg@17
    33
	private String name;
franta-hg@17
    34
	private long offset;
franta-hg@17
    35
	private long length;
franta-hg@17
    36
	private long ordinal;
franta-hg@17
    37
franta-hg@17
    38
	public IndexEntry(String name, long offset, long length) {
franta-hg@17
    39
		this.name = name;
franta-hg@17
    40
		this.offset = offset;
franta-hg@17
    41
		this.length = length;
franta-hg@17
    42
	}
franta-hg@17
    43
franta-hg@17
    44
	public void serialize(DataOutputStream indexOutputStream) throws IOException {
franta-hg@17
    45
		indexOutputStream.write(name.getBytes(StandardCharsets.UTF_8));
franta-hg@17
    46
		indexOutputStream.write(0);
franta-hg@17
    47
		indexOutputStream.writeInt((int) offset); // unsigned int 32
franta-hg@17
    48
		indexOutputStream.writeInt((int) length); // unsigned int 32
franta-hg@17
    49
	}
franta-hg@17
    50
franta-hg@17
    51
	public void setOrdinal(long ordinal) {
franta-hg@17
    52
		this.ordinal = ordinal;
franta-hg@17
    53
	}
franta-hg@17
    54
franta-hg@17
    55
	public long getOrdinal() {
franta-hg@17
    56
		return ordinal;
franta-hg@17
    57
	}
franta-hg@17
    58
franta-hg@15
    59
	@Override
franta-hg@15
    60
	public int compareTo(IndexEntry o) {
franta-hg@17
    61
		int nameDiff = name.compareTo(o.name);
franta-hg@17
    62
		if (nameDiff == 0) {
franta-hg@17
    63
			int offsetDiff = ((Long) offset).compareTo(o.offset);
franta-hg@17
    64
			if (offsetDiff == 0) {
franta-hg@17
    65
				return ((Long) length).compareTo(o.length);
franta-hg@17
    66
			} else {
franta-hg@17
    67
				return offsetDiff;
franta-hg@17
    68
			}
franta-hg@17
    69
		} else {
franta-hg@17
    70
			return nameDiff;
franta-hg@17
    71
		}
franta-hg@17
    72
	}
franta-hg@17
    73
franta-hg@17
    74
	@Override
franta-hg@17
    75
	public boolean equals(Object o) {
franta-hg@17
    76
		return o instanceof IndexEntry && compareTo((IndexEntry) o) == 0;
franta-hg@17
    77
	}
franta-hg@17
    78
franta-hg@17
    79
	@Override
franta-hg@17
    80
	public int hashCode() {
franta-hg@17
    81
		int hash = 5;
franta-hg@17
    82
		hash = 53 * hash + Objects.hashCode(this.name);
franta-hg@17
    83
		hash = 53 * hash + (int) (this.offset ^ (this.offset >>> 32));
franta-hg@17
    84
		hash = 53 * hash + (int) (this.length ^ (this.length >>> 32));
franta-hg@17
    85
		return hash;
franta-hg@15
    86
	}
franta-hg@15
    87
}