X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fentity%2FTag.java;fp=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fentity%2FTag.java;h=2c0a3808f6205035ef7f1139df42d22a3cd29384;hp=0000000000000000000000000000000000000000;hb=5d271f23371d6a435a0435f000370f4d9b8c621a;hpb=569af88b0115065e1a4551a20501bbd200fcba17 diff --git a/chesshog-format/src/main/java/org/hedgecode/chess/pgn/entity/Tag.java b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/entity/Tag.java new file mode 100644 index 0000000..2c0a380 --- /dev/null +++ b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/entity/Tag.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2018-2020. Developed by Hedgecode. + * + * 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 org.hedgecode.chess.pgn.entity; + +import java.util.Arrays; +import java.util.Comparator; + +import org.hedgecode.chess.pgn.PGNUtils; + +/** + * Tag + * + * @author Dmitry Samoshin aka gotty + */ +public enum Tag { + + EVENT ( "Event", true, Tag.QUESTION ), + SITE ( "Site", true, Tag.QUESTION ), + DATE ( "Date", true, Tag.QUESTION_DATE ), + ROUND ( "Round", true, Tag.QUESTION ), + WHITE ( "White", true, Tag.QUESTION ), + BLACK ( "Black", true, Tag.QUESTION ), + RESULT ( "Result", true, Tag.NON_RESULT ), + WHITE_TITLE ( "WhiteTitle", false, Tag.HYPHEN ), + BLACK_TITLE ( "BlackTitle", false, Tag.HYPHEN ), + WHITE_ELO ( "WhiteElo", false, Tag.HYPHEN ), + BLACK_ELO ( "BlackElo", false, Tag.HYPHEN ), + WHITE_USCF ( "WhiteUSCF", false, Tag.HYPHEN ), + BLACK_USCF ( "BlackUSCF", false, Tag.HYPHEN ), + WHITE_NA ( "WhiteNA", false, Tag.HYPHEN ), + BLACK_NA ( "BlackNA", false, Tag.HYPHEN ), + WHITE_TYPE ( "WhiteType", false, Tag.HYPHEN ), + BLACK_TYPE ( "BlackType", false, Tag.HYPHEN ), + EVENT_DATE ( "EventDate", false, Tag.QUESTION_DATE ), + EVENT_SPONSOR ( "EventSponsor", false, Tag.EMPTY ), + SECTION ( "Section", false, Tag.EMPTY ), + STAGE ( "Stage", false, Tag.EMPTY ), + BOARD ( "Board", false, Tag.EMPTY ), + OPENING ( "Opening", false, Tag.EMPTY ), + VARIATION ( "Variation", false, Tag.EMPTY ), + SUBVARIATION ( "SubVariation", false, Tag.EMPTY ), + ECO ( "ECO", false, Tag.EMPTY ), + NIC ( "NIC", false, Tag.EMPTY ), + TIME ( "Time", false, Tag.QUESTION_TIME ), + UTC_TIME ( "UTCTime", false, Tag.QUESTION_TIME ), + UTC_DATE ( "UTCDate", false, Tag.QUESTION_DATE ), + TIME_CONTROL ( "TimeControl", false, Tag.QUESTION ), + SETUP ( "SetUp", false, Tag.ZERO ), + FEN ( "FEN", false, Tag.EMPTY ), + TERMINATION ( "Termination", false, Tag.EMPTY ), + ANNOTATOR ( "Annotator", false, Tag.EMPTY ), + MODE ( "Mode", false, Tag.EMPTY ), + PLY_COUNT ( "PlyCount", false, Tag.EMPTY ); + + // todo: -> PGNConstants + public static final String EMPTY = ""; + public static final String HYPHEN = "-"; + public static final String ZERO = "0"; + public static final String QUESTION = "?"; + public static final String QUESTION_DATE = "????.??.??"; + public static final String QUESTION_TIME = "??:??:??"; + public static final String NON_RESULT = "*"; + + public static final String TAG_FORMAT = "[%s \"%s\"]"; + + public static final char[] TAG_SHIELD_CHARS = { '\\', '"' }; + + private String tagName; + private boolean isRequired; + private String defaultValue; + + Tag(String name, boolean required, String defValue) { + tagName = name; + isRequired = required; + defaultValue = defValue; + } + + public String getName() { + return tagName; + } + + public boolean isRequired() { + return isRequired; + } + + public String defaultValue() { + return defaultValue; + } + + public static String formatTagValue(String tagValue) { + return PGNUtils.shield( + tagValue, TAG_SHIELD_CHARS + ); + } + + public static Tag[] tags() { + Tag[] tags = values(); + Arrays.sort( + tags, new TagComparator() + ); + return tags; + } + + static class TagComparator implements Comparator { + + @Override + public int compare(Tag tag1, Tag tag2) { + if (tag1.isRequired && tag2.isRequired) { + return tag1.ordinal() - tag2.ordinal(); + } else { + return tag1.isRequired ? -1 + : tag2.isRequired ? 1 + : tag1.name().compareTo(tag2.name()); + } + } + + } + +}