From fd618395946a050679707251fbff105e9340c240 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Reimar=20D=C3=B6ffinger?= Date: Sun, 20 May 2018 14:41:48 +0200 Subject: [PATCH] Move several files out of Util. --- compile.sh | 2 +- .../dictionary/engine/EntryTypeName.java | 84 +++++++++++++++++++ src/com/hughes/util/Args.java | 35 ++++++++ src/com/hughes/util/EnumUtil.java | 31 +++++++ src/com/hughes/util/FileUtil.java | 67 +++++++++++++++ src/com/hughes/util/MapUtil.java | 55 ++++++++++++ 6 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 src/com/hughes/android/dictionary/engine/EntryTypeName.java create mode 100644 src/com/hughes/util/Args.java create mode 100644 src/com/hughes/util/EnumUtil.java create mode 100644 src/com/hughes/util/FileUtil.java create mode 100644 src/com/hughes/util/MapUtil.java diff --git a/compile.sh b/compile.sh index 5129fc9..95f9686 100755 --- a/compile.sh +++ b/compile.sh @@ -31,4 +31,4 @@ if [ ! -r "$COMMONS_COMPRESS" ] ; then echo "commons-compress needs to be installed" exit 1; fi -javac -g ../Dictionary/Util/src/com/hughes/util/*.java ../Dictionary/Util/src/com/hughes/util/raf/*.java ../Dictionary/src/com/hughes/android/dictionary/DictionaryInfo.java ../Dictionary/src/com/hughes/android/dictionary/engine/*.java ../Dictionary/src/com/hughes/android/dictionary/C.java src/com/hughes/android/dictionary/*.java src/com/hughes/android/dictionary/*/*.java src/com/hughes/android/dictionary/*/*/*.java -classpath "$ICU4J:$JUNIT:$XERCES:$COMMONS:$COMMONS_COMPRESS" +javac -g ../Dictionary/Util/src/com/hughes/util/*.java ../Dictionary/Util/src/com/hughes/util/raf/*.java ../Dictionary/src/com/hughes/android/dictionary/DictionaryInfo.java ../Dictionary/src/com/hughes/android/dictionary/engine/*.java ../Dictionary/src/com/hughes/android/dictionary/C.java src/com/hughes/util/*.java src/com/hughes/android/dictionary/*.java src/com/hughes/android/dictionary/*/*.java src/com/hughes/android/dictionary/*/*/*.java -classpath "$ICU4J:$JUNIT:$XERCES:$COMMONS:$COMMONS_COMPRESS" diff --git a/src/com/hughes/android/dictionary/engine/EntryTypeName.java b/src/com/hughes/android/dictionary/engine/EntryTypeName.java new file mode 100644 index 0000000..4206265 --- /dev/null +++ b/src/com/hughes/android/dictionary/engine/EntryTypeName.java @@ -0,0 +1,84 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.android.dictionary.engine; + +public enum EntryTypeName { + + WIKTIONARY_TITLE_SINGLE_DETAIL(true, true, null), + WIKTIONARY_TITLE_SINGLE(true, true, null), + WIKTIONARY_INFLECTD_FORM_SINGLE(false, true, null), + + ONE_WORD(true, true, null), + MULTIROW_HEAD_ONE_WORD(true, true, null), + MULTIROW_TAIL_ONE_WORD(false, true, null), + + SYNONYM_SINGLE(false, true, null), + ANTONYM_SINGLE(false, true, null), + + WIKTIONARY_TITLE_MULTI_DETAIL(false, true, WIKTIONARY_TITLE_SINGLE_DETAIL), + WIKTIONARY_TITLE_MULTI(false, true, WIKTIONARY_TITLE_SINGLE), + WIKTIONARY_TRANSLITERATION(), + // How we file "casa {f}, case {pl}" under "case" + WIKTIONARY_INFLECTED_FORM_MULTI(false, true, WIKTIONARY_INFLECTD_FORM_SINGLE), + WIKTIONARY_ENGLISH_DEF_WIKI_LINK(), + WIKTIONARY_ENGLISH_DEF_OTHER_LANG(), + WIKTIONARY_ENGLISH_DEF(), + + SYNONYM_MULTI(false, true, SYNONYM_SINGLE), + ANTONYM_MULTI(false, true, ANTONYM_SINGLE), + DERIVED_TERM(false, true, null), + + TWO_WORDS(), + THREE_WORDS(), + FOUR_WORDS(), + FIVE_OR_MORE_WORDS(), + WIKTIONARY_TRANSLATION_WIKI_TEXT(), + WIKTIONARY_TRANSLATION_OTHER_TEXT(), + + // How we file entries like: "sono: {form of|essere}" under "sono.". + WIKTIONARY_IS_FORM_OF_SOMETHING_ELSE(false, true, null), + + MULTIROW_HEAD_MANY_WORDS(), + MULTIROW_TAIL_MANY_WORDS(), + WIKTIONARY_EXAMPLE(), + + // The next two are how we file entries like: "sono: {form of|essere}" under + // "essere". + WIKTIONARY_BASE_FORM_SINGLE(), // These two should be eligible for removal + // if the links are otherwise present. + WIKTIONARY_BASE_FORM_MULTI(false, false, WIKTIONARY_BASE_FORM_SINGLE), + PART_OF_HYPHENATED(), + BRACKETED(), + PARENTHESIZED(), + WIKTIONARY_TRANSLATION_SENSE(), + SEE_ALSO(), + WIKTIONARY_MENTIONED(false, true, null), ; + + final boolean mainWord; + final boolean overridesStopList; + final EntryTypeName singleWordInstance; + + EntryTypeName() { + this(false, false, null); + } + + EntryTypeName(final boolean mainWord, final boolean overridesStopList, + final EntryTypeName singleWordInstance) { + this.mainWord = mainWord; + this.overridesStopList = overridesStopList; + this.singleWordInstance = singleWordInstance == null ? this : singleWordInstance; + } + +} diff --git a/src/com/hughes/util/Args.java b/src/com/hughes/util/Args.java new file mode 100644 index 0000000..6c35dd1 --- /dev/null +++ b/src/com/hughes/util/Args.java @@ -0,0 +1,35 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.util; + +import java.util.LinkedHashMap; +import java.util.Map; + +@SuppressWarnings("WeakerAccess") +public class Args { + + public static Map keyValueArgs(final String[] args) { + final Map dest = new LinkedHashMap<>(); + for (String arg : args) { + int equalsIndex; + if (arg.startsWith("--") && (equalsIndex = arg.indexOf("=")) >= 0) { + final String key = arg.substring(2, equalsIndex); + final String value = arg.substring(equalsIndex + 1, arg.length()); + dest.put(key, value); + } + } + return dest; + } +} diff --git a/src/com/hughes/util/EnumUtil.java b/src/com/hughes/util/EnumUtil.java new file mode 100644 index 0000000..4cd956b --- /dev/null +++ b/src/com/hughes/util/EnumUtil.java @@ -0,0 +1,31 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.util; + + +@SuppressWarnings("WeakerAccess") +public final class EnumUtil { + + public static final > T min(final T e1, final T e2) { + if (e1 == null) { + return e2; + } + if (e2 == null) { + return e1; + } + return e1.compareTo(e2) < 0 ? e1 : e2; + } + +} diff --git a/src/com/hughes/util/FileUtil.java b/src/com/hughes/util/FileUtil.java new file mode 100644 index 0000000..d3024da --- /dev/null +++ b/src/com/hughes/util/FileUtil.java @@ -0,0 +1,67 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.util; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintStream; +import java.io.RandomAccessFile; +import java.util.ArrayList; +import java.util.List; + +@SuppressWarnings("WeakerAccess") +public final class FileUtil { + public static String readLine(final RandomAccessFile file, final long startPos) throws IOException { + file.seek(startPos); + return file.readLine(); + } + + public static List readLines(final File file) throws IOException { + final List result = new ArrayList<>(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { + String line; + while ((line = in.readLine()) != null) { + result.add(line); + } + } + return result; + } + + public static String readToString(final File file) throws IOException { + StringBuilder result = new StringBuilder(); + try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) { + String line; + while ((line = in.readLine()) != null) { + result.append(line).append("\n"); + } + } + return result.toString(); + } + + public static void writeStringToUTF8File(final String string, final File file) { + throw new IllegalStateException(); + } + + public static void printString(final File file, final String s) throws IOException { + final PrintStream out = new PrintStream(new FileOutputStream(file)); + out.print(s); + out.close(); + } + +} diff --git a/src/com/hughes/util/MapUtil.java b/src/com/hughes/util/MapUtil.java new file mode 100644 index 0000000..a62a86e --- /dev/null +++ b/src/com/hughes/util/MapUtil.java @@ -0,0 +1,55 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.util; + +import java.util.Map; + +@SuppressWarnings({"WeakerAccess", "unused"}) +public class MapUtil { + + public static V safeGet(final Map map, K key, V defaultValue) { + if (!map.containsKey(key)) { + return defaultValue; + } + return map.get(key); + } + + public static V safeGetOrPut(final Map map, K key, V defaultValue) { + if (!map.containsKey(key)) { + map.put(key, defaultValue); + } + return map.get(key); + } + + public static V safeGet(final Map map, K key, Class valueClass) { + if (!map.containsKey(key)) { + try { + map.put(key, valueClass.newInstance()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + return map.get(key); + } + + public static V safeRemove(final Map map, K key, V defaultValue) { + if (!map.containsKey(key)) { + return defaultValue; + } + return map.remove(key); + } + + +} -- 2.43.0