001package ball.game.crossword;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * %%
006 * Copyright (C) 2010 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.Comparator;
022import java.util.Objects;
023
024/**
025 * Crossword clue {@link Label}.
026 *
027 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
028 */
029public class Label implements Comparable<Label> {
030    private static final Comparator<? super Label> COMPARATOR =
031        Comparator
032        .<Label>comparingInt(t -> t.direction.ordinal())
033        .thenComparingInt(Label::getIndex);
034
035    private final Direction direction;
036    private final int index;
037
038    /**
039     * @param   direction       The solution word {@link Direction}.
040     * @param   index           The starting block index.
041     */
042    protected Label(Direction direction, int index) {
043        this.direction = Objects.requireNonNull(direction);
044        this.index = index;
045    }
046
047    public Direction getDirection() { return direction; }
048
049    public int getIndex() { return index; }
050
051    @Override
052    public int compareTo(Label that) {
053        return Objects.compare(this, that, COMPARATOR);
054    }
055
056    @Override
057    public boolean equals(Object object) {
058        return ((object instanceof Label) ? (this.compareTo((Label) object) == 0) : super.equals(object));
059    }
060
061    @Override
062    public int hashCode() { return Objects.hash(direction, index); }
063
064    @Override
065    public String toString() {
066        return (getDirection().toString().substring(0, 1) + String.valueOf(index));
067    }
068
069    public static Label parse(String string) {
070        return new Label(Direction.parse(string.substring(0, 1)), Integer.parseInt(string.substring(1)));
071    }
072}