blob: a532865090e7877600eb1e57782411fddb6d1257 [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
Constantin Ziesche02817f12020-08-04 21:40:43 +020053 public static KeyElements GetKeyElementFromType(Type type)
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010054 {
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;
Constantin Ziesche02817f12020-08-04 21:40:43 +020071 else if (typeof(IReferenceElement).IsAssignableFrom(type))
72 return KeyElements.ReferenceElement;
73 else if (typeof(IRange).IsAssignableFrom(type))
74 return KeyElements.Range;
75 else if (typeof(IOperation).IsAssignableFrom(type))
76 return KeyElements.Operation;
77 else if (typeof(IRelationshipElement).IsAssignableFrom(type))
78 return KeyElements.RelationshipElement;
79 else if (typeof(IAnnotatedRelationshipElement).IsAssignableFrom(type))
80 return KeyElements.AnnotatedRelationshipElement;
81 else if (typeof(IEvent).IsAssignableFrom(type))
82 return KeyElements.Event;
83 else if (typeof(IBasicEvent).IsAssignableFrom(type))
84 return KeyElements.BasicEvent;
85 else if (typeof(IFile).IsAssignableFrom(type))
86 return KeyElements.File;
87 else if (typeof(IBlob).IsAssignableFrom(type))
88 return KeyElements.Blob;
89 else if (typeof(ISubmodelElementCollection).IsAssignableFrom(type))
90 return KeyElements.SubmodelElementCollection;
91 else if (typeof(IEntity).IsAssignableFrom(type))
92 return KeyElements.Entity;
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010093 else
94 throw new InvalidOperationException("Cannot convert type " + type.FullName + "to referable element");
95 }
96
Constantin Ziesche02817f12020-08-04 21:40:43 +020097 public string ToStandardizedString()
98 {
99 return string.Format("({0})({1})[{2}]{3}", Type, Local ? "local" : "no-local", IdType, Value);
100 }
101
Constantin Ziesche857c7ab2020-02-25 11:24:51 +0100102 #region IEquatable
103 public bool Equals(Key other)
104 {
105 if (ReferenceEquals(null, other))
106 {
107 return false;
108 }
109 if (ReferenceEquals(this, other))
110 {
111 return true;
112 }
113
114 return this.IdType.Equals(other.IdType)
115 && this.Local.Equals(other.Local)
116 && this.Type.Equals(other.Type)
117 && this.Value.Equals(other.Type);
118 }
119 public override bool Equals(object obj)
120 {
121 if (ReferenceEquals(null, obj))
122 {
123 return false;
124 }
125 if (ReferenceEquals(this, obj))
126 {
127 return true;
128 }
129
130 return obj.GetType() == GetType() && Equals((Key)obj);
131 }
132
133
134 public override int GetHashCode()
135 {
136 unchecked
137 {
138 var result = 0;
139 result = (result * 397) ^ IdType.GetHashCode();
140 result = (result * 397) ^ Type.GetHashCode();
141 result = (result * 397) ^ (Local ? 1 : 0);
142 return result;
143 }
144 }
145
146 public static bool operator ==(Key x, Key y)
147 {
148
149 if (ReferenceEquals(x, y))
150 {
151 return true;
152 }
153
154 if (ReferenceEquals(x, null))
155 {
156 return false;
157 }
158 if (ReferenceEquals(y, null))
159 {
160 return false;
161 }
162
163 return x.IdType == y.IdType
164 && x.Local == y.Local
165 && x.Type == y.Type
166 && x.Value == y.Value;
167 }
168 public static bool operator !=(Key x, Key y)
169 {
170 return !(x == y);
171 }
172 #endregion
173 }
174
175 [DataContract]
176 public class Key<T> : Key
177 {
Constantin Ziesche02817f12020-08-04 21:40:43 +0200178 public Key(KeyType idType, string value, bool local) : base(GetKeyElementFromType(typeof(T)), idType, value, local)
179 { }
Constantin Ziesche857c7ab2020-02-25 11:24:51 +0100180 }
181
182}