001package ball.game.crossword; 002/*- 003 * ########################################################################## 004 * Game Applications and Utilities 005 * $Id: Direction.java 5285 2020-02-05 04:23:21Z ball $ 006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/crossword/Direction.java $ 007 * %% 008 * Copyright (C) 2010 - 2020 Allen D. Ball 009 * %% 010 * Licensed under the Apache License, Version 2.0 (the "License"); 011 * you may not use this file except in compliance with the License. 012 * You may obtain a copy of the License at 013 * 014 * http://www.apache.org/licenses/LICENSE-2.0 015 * 016 * Unless required by applicable law or agreed to in writing, software 017 * distributed under the License is distributed on an "AS IS" BASIS, 018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 019 * See the License for the specific language governing permissions and 020 * limitations under the License. 021 * ########################################################################## 022 */ 023import ball.util.CoordinateMap; 024import java.util.Map; 025import java.util.TreeMap; 026import java.util.stream.Stream; 027 028import static java.util.Collections.unmodifiableMap; 029 030/** 031 * Crossword clue {@link Direction}. 032 * 033 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 034 * @version $Revision: 5285 $ 035 */ 036public enum Direction { 037 ACROSS, DOWN; 038 039 private static final Map<String,Direction> MAP; 040 041 static { 042 TreeMap<String,Direction> map = 043 new TreeMap<>(String.CASE_INSENSITIVE_ORDER); 044 045 Stream.of(values()) 046 .forEach(t -> map.put(t.name().substring(0, 1), t)); 047 048 MAP = unmodifiableMap(map); 049 } 050 051 /** 052 * Static method to parse a {@link String} consistent with 053 * {@link #name()} and {@link #toString()} to a {@link Direction}. 054 * 055 * @param string The {@link String} to parse. 056 * 057 * @return The {@link Direction}. 058 */ 059 public static Direction parse(String string) { 060 Direction direction = MAP.get(string); 061 062 if (direction == null) { 063 direction = Enum.valueOf(Direction.class, string); 064 } 065 066 return direction; 067 } 068 069 /** 070 * Static method to analyze {@link CoordinateMap} to determine a 071 * {@link Direction}. 072 * 073 * @param map The {@link CoordinateMap} to analyze. 074 * 075 * @return The {@link Direction}. 076 * 077 * @throws IllegalArgumentException 078 * If both {@link CoordinateMap#getRowCount()} 079 * and {@link CoordinateMap#getColumnCount()} 080 * are not equal to {@code 1}. 081 */ 082 public static Direction of(CoordinateMap<?> map) { 083 Direction direction = null; 084 085 if (map.getRowCount() > 1) { 086 direction = Direction.DOWN; 087 } else if (map.getColumnCount() > 1) { 088 direction = Direction.ACROSS; 089 } else { 090 throw new IllegalArgumentException(); 091 } 092 093 return direction; 094 } 095}