001package Torello.Browser.JsonAST;
002
003import Torello.JSON.ReadJSON;
004
005import javax.json.JsonObject;
006
007/**
008 * Extracts the Json Property {@code "name"} from the input {@link JsonObject}.  When building the 
009 * AST from the CDP JSON spec files, objecs which are located in a {@link Domain Domain's} 
010 * {@code "types"} array will differ slightly in that they will not have a {@code "name"} property
011 * at all.  Intead {@code "types"}, which are constructed into instances of class {@link TypeNode},
012 * will have an Json Property named {@link TypeNode#name "id"} instead.
013 */
014@Torello.JavaDoc.Annotations.StaticFunctional
015@Torello.JavaDoc.Annotations.JDHeaderBackgroundImg(EmbedTagFileID="CONSTRUCTOR_JDHBI")
016public class Helper$Name 
017{
018    private Helper$Name() { }
019
020    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
021    // this.name  **OR**  this.id 
022    // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
023
024    static String getName(final Entity THIS, final JsonObject jo)
025    {
026        // getString(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
027        final String name   = ReadJSON.getString(jo, "name",    true, true);
028        final String id     = ReadJSON.getString(jo, "id",      true, true);
029
030        final int count =
031            ((name      == null) ? 0 : 1) + // For a "Command TCE", "Event TCE", and **ALL** PPR
032            ((id        == null) ? 0 : 1);  // For a "Type TCE"
033
034        if (count != 1) THIS.verifyThrow(
035            "Precisely one of these properties MUST contain a NON-NULL String:\n" +
036            "name: [" + name + "], id: [" + id + "]\n" +
037            "Apparently there were [" + count + "] set."
038        );
039
040        return (id != null)
041            ? id    // Sub-Class is guaranteed to be a "Type TCE"
042            : name; // **ANY** PPR, a "Command-TCE" or "Event-TCE"
043    }
044}