blob: 2e1eee97f15199e9219359501f15dcf007684f86 [file] [log] [blame]
Constantin Ziesche857c7ab2020-02-25 11:24:51 +01001/*******************************************************************************
2* Copyright (c) 2020 Robert Bosch GmbH
3* Author: Constantin Ziesche (constantin.ziesche@bosch.com)
4*
5* This program and the accompanying materials are made available under the
6* terms of the Eclipse Public License 2.0 which is available at
7* http://www.eclipse.org/legal/epl-2.0
8*
9* SPDX-License-Identifier: EPL-2.0
10*******************************************************************************/
11using BaSyx.Models.Core.AssetAdministrationShell.Generics;
12using BaSyx.Models.Core.AssetAdministrationShell.Generics.SubmodelElementTypes;
13using BaSyx.Models.Core.AssetAdministrationShell.Semantics;
14using BaSyx.Models.Core.AssetAdministrationShell.Views;
15using Newtonsoft.Json;
16using System;
17using System.Runtime.Serialization;
18using System.Xml.Serialization;
19
20namespace BaSyx.Models.Core.AssetAdministrationShell.References
21{
22 [DataContract]
23 [XmlType("key")]
24 public class Key : IKey, IEquatable<Key>
25 {
26 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
27 [XmlAttribute("type")]
28 public KeyElements Type { get; set; }
29
30 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
31 [XmlAttribute("idType")]
32 public KeyType IdType { get; set; }
33
34 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
35 [XmlText]
36 public string Value { get; set; }
37
38 [JsonProperty(Required = Required.Always, DefaultValueHandling = DefaultValueHandling.Include)]
39 [XmlAttribute("local")]
40 public bool Local { get; set; }
41
42 internal Key() { }
43
44 [JsonConstructor]
45 public Key(KeyElements type, KeyType idType, string value, bool local)
46 {
47 Type = type;
48 IdType = idType;
49 Value = value;
50 Local = local;
51 }
52
53 public static KeyElements GetReferableElement(Type type)
54 {
55 if (typeof(IAsset).IsAssignableFrom(type))
56 return KeyElements.Asset;
57 else if (typeof(IAssetAdministrationShell).IsAssignableFrom(type))
58 return KeyElements.AssetAdministrationShell;
59 else if (typeof(ISubmodel).IsAssignableFrom(type))
60 return KeyElements.Submodel;
61 else if (typeof(IView).IsAssignableFrom(type))
62 return KeyElements.View;
63 else if (typeof(IProperty).IsAssignableFrom(type))
64 return KeyElements.Property;
65 else if (typeof(IOperation).IsAssignableFrom(type))
66 return KeyElements.Operation;
67 else if (typeof(IEvent).IsAssignableFrom(type))
68 return KeyElements.Event;
69 else if (typeof(IConceptDescription).IsAssignableFrom(type))
70 return KeyElements.ConceptDescription;
71 else
72 throw new InvalidOperationException("Cannot convert type " + type.FullName + "to referable element");
73 }
74
75 #region IEquatable
76 public bool Equals(Key other)
77 {
78 if (ReferenceEquals(null, other))
79 {
80 return false;
81 }
82 if (ReferenceEquals(this, other))
83 {
84 return true;
85 }
86
87 return this.IdType.Equals(other.IdType)
88 && this.Local.Equals(other.Local)
89 && this.Type.Equals(other.Type)
90 && this.Value.Equals(other.Type);
91 }
92 public override bool Equals(object obj)
93 {
94 if (ReferenceEquals(null, obj))
95 {
96 return false;
97 }
98 if (ReferenceEquals(this, obj))
99 {
100 return true;
101 }
102
103 return obj.GetType() == GetType() && Equals((Key)obj);
104 }
105
106
107 public override int GetHashCode()
108 {
109 unchecked
110 {
111 var result = 0;
112 result = (result * 397) ^ IdType.GetHashCode();
113 result = (result * 397) ^ Type.GetHashCode();
114 result = (result * 397) ^ (Local ? 1 : 0);
115 return result;
116 }
117 }
118
119 public static bool operator ==(Key x, Key y)
120 {
121
122 if (ReferenceEquals(x, y))
123 {
124 return true;
125 }
126
127 if (ReferenceEquals(x, null))
128 {
129 return false;
130 }
131 if (ReferenceEquals(y, null))
132 {
133 return false;
134 }
135
136 return x.IdType == y.IdType
137 && x.Local == y.Local
138 && x.Type == y.Type
139 && x.Value == y.Value;
140 }
141 public static bool operator !=(Key x, Key y)
142 {
143 return !(x == y);
144 }
145 #endregion
146 }
147
148 [DataContract]
149 public class Key<T> : Key
150 {
151 public Key(KeyType idType, string value, bool local) : base(GetReferableElement(typeof(T)), idType, value, local)
152 { }
153 }
154
155}