java/dictionary-generator/src/cz/frantovo/telco/dictionary/SynonymsEntry.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Oct 2024 20:02:11 +0200
changeset 157 7abf9048d38d
parent 151 a9f1ba451247
permissions -rw-r--r--
data: FXO and FXS modules on Digium/Asterisk analog telephony cards
     1 /**
     2  * Free Telco Dictionary
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    16  */
    17 package cz.frantovo.telco.dictionary;
    18 
    19 import java.io.DataOutputStream;
    20 import java.io.IOException;
    21 import java.nio.charset.StandardCharsets;
    22 import java.util.Objects;
    23 
    24 /**
    25  * Represents one item in StarDict synonyms file (.syn)
    26  * links the synonym term (string) to the position of the base term in index file
    27  * 
    28  * @author Ing. František Kučera (frantovo.cz)
    29  */
    30 public class SynonymsEntry implements Comparable<SynonymsEntry> {
    31 
    32 	private IndexEntry base;
    33 	private String name;
    34 
    35 	public SynonymsEntry(IndexEntry base, String name) {
    36 		this.base = base;
    37 		this.name = name;
    38 	}
    39 
    40 	public void serialize(DataOutputStream synonymOutputStream) throws IOException {
    41 		synonymOutputStream.write(name.getBytes(StandardCharsets.UTF_8));
    42 		synonymOutputStream.write(0);
    43 		synonymOutputStream.writeInt((int) base.getOrdinal()); // unsigned int 32
    44 	}
    45 
    46 	@Override
    47 	public int compareTo(SynonymsEntry o) {
    48 		int nameDiff = name.compareTo(o.name);
    49 		if (nameDiff == 0) {
    50 			return base.compareTo(o.base);
    51 		} else {
    52 			return nameDiff;
    53 		}
    54 	}
    55 
    56 	@Override
    57 	public boolean equals(Object o) {
    58 		return o instanceof IndexEntry && compareTo((SynonymsEntry) o) == 0;
    59 	}
    60 
    61 	@Override
    62 	public int hashCode() {
    63 		int hash = 3;
    64 		hash = 47 * hash + Objects.hashCode(this.base);
    65 		hash = 47 * hash + Objects.hashCode(this.name);
    66 		return hash;
    67 	}
    68 }