1 package org.djutils.metadata.demo;
2
3 import org.djutils.metadata.MetaData;
4 import org.djutils.metadata.ObjectDescriptor;
5
6 /**
7 * MetaDataDemo.java. <br>
8 * <br>
9 * Copyright (c) 2020-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
11 * distributed under a three-clause BSD-style license, which can be found at
12 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
13 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14 * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
15 */
16 public final class MetaDataDemo
17 {
18 /**
19 * Do not instantiate.
20 */
21 private MetaDataDemo()
22 {
23 // Do not instantiate
24 }
25
26 /**
27 * Demonstration of MetaData use.
28 * @param args String[]; the command line arguments; not used
29 */
30 public static void main(final String[] args)
31 {
32 try
33 {
34 testSingleLong();
35 }
36 catch (RuntimeException e)
37 {
38 e.printStackTrace();
39 }
40
41 try
42 {
43 testObjectArray();
44 }
45 catch (RuntimeException e)
46 {
47 e.printStackTrace();
48 }
49
50 }
51
52 /**
53 * Single object meta data that only permits a Long.
54 * @throws RuntimeException ...
55 */
56 static void testSingleLong() throws RuntimeException
57 {
58 MetaData singleLong = new MetaData("Payload is one Long", "Payload is one Long; anything else is wrong",
59 new ObjectDescriptor("Long", "64 bit Long", Long.class));
60 singleLong.verifyComposition(123L); // OK
61 singleLong.verifyComposition(123); // Throws ClassCastException
62 }
63
64 /**
65 * MetaData demo using array of Object.
66 * @throws RuntimeException ...
67 */
68 static void testObjectArray() throws RuntimeException
69 {
70 MetaData objectArray = new MetaData("Payload is object array",
71 "Payload is an object array of 3 objects; a String, a Double and an Integer",
72 new ObjectDescriptor[] {new ObjectDescriptor("String", "String", String.class),
73 new ObjectDescriptor("Double", "Double", Double.class),
74 new ObjectDescriptor("Integer", "Integer", Integer.class)});
75 Object[] payload = new Object[] {"This is the string", 123.456, 987};
76 objectArray.verifyComposition(payload); // OK
77 payload = new Object[] {"String", "123.456", 987L};
78 objectArray.verifyComposition(payload); // Throws ClassCastException
79 }
80
81 }