|
| 1 | +/* |
| 2 | + * This file is part of the Cliche project, licensed under MIT License. See LICENSE.txt file in root folder of Cliche |
| 3 | + * sources. |
| 4 | + */ |
| 5 | + |
| 6 | +package net.dudehook.cliche2; |
| 7 | + |
| 8 | +import java.lang.reflect.Method; |
| 9 | +import java.util.Arrays; |
| 10 | + |
| 11 | +/** |
| 12 | + * This interface is a Strategy for auto-naming commands with no name specified based on command's method. The algorithm |
| 13 | + * is isolated because it's highly subjective and therefore subject to changes. And, if you don't like default |
| 14 | + * dash-joined naming you can completely redefine the functionality. |
| 15 | + * |
| 16 | + * It is also responsible for generating several suggested abbreviations, one from them may later be "approved" by |
| 17 | + * CommandTable. |
| 18 | + * |
| 19 | + * @author ASG |
| 20 | + */ |
| 21 | +public interface CommandNamer |
| 22 | +{ |
| 23 | + |
| 24 | + /** |
| 25 | + * Generate command name and suggested abbreviation variants. Since this method is given a Method, not a string, the |
| 26 | + * algorithm can take parameter types into account. |
| 27 | + * |
| 28 | + * @param commandMethod |
| 29 | + * Command method |
| 30 | + * @return asg.cliche.CommandNamer.NamingInfo containing generated name and abbrev array. |
| 31 | + */ |
| 32 | + NamingInfo nameCommand(Method commandMethod); |
| 33 | + |
| 34 | + /** |
| 35 | + * Return value grouping structure for nameCommand(). I decided to return name and abbreviations together because in |
| 36 | + * the default algorithm they are generated simultaneously, and I think this approach is better than having a |
| 37 | + * stateful strategy. |
| 38 | + */ |
| 39 | + public static class NamingInfo |
| 40 | + { |
| 41 | + public final String commandName; |
| 42 | + public final String[] possibleAbbreviations; |
| 43 | + |
| 44 | + public NamingInfo(String commandName, String[] possibleAbbreviations) |
| 45 | + { |
| 46 | + this.commandName = commandName; |
| 47 | + this.possibleAbbreviations = possibleAbbreviations; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean equals(Object obj) |
| 52 | + { |
| 53 | + if (obj == null) |
| 54 | + { |
| 55 | + return false; |
| 56 | + } |
| 57 | + if (getClass() != obj.getClass()) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + final NamingInfo other = (NamingInfo)obj; |
| 62 | + if ((this.commandName == null) ? (other.commandName != null) : !this.commandName.equals(other.commandName)) |
| 63 | + { |
| 64 | + return false; |
| 65 | + } |
| 66 | + if (this.possibleAbbreviations != other.possibleAbbreviations |
| 67 | + && (this.possibleAbbreviations == null || !Arrays.equals(this.possibleAbbreviations, other.possibleAbbreviations))) |
| 68 | + { |
| 69 | + return false; |
| 70 | + } |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode() |
| 76 | + { |
| 77 | + int hash = 5; |
| 78 | + hash = 59 * hash + (this.commandName != null ? this.commandName.hashCode() : 0); |
| 79 | + hash = 59 * hash + (this.possibleAbbreviations != null ? Arrays.hashCode(this.possibleAbbreviations) : 0); |
| 80 | + return hash; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String toString() |
| 85 | + { |
| 86 | + return String.format("NamingInfo(%s, %s)", commandName, Arrays.toString(possibleAbbreviations)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments