| /******************************************************************************* |
| * Copyright (c) 2020 Robert Bosch GmbH |
| * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0 |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| *******************************************************************************/ |
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Text; |
| |
| namespace BaSyx.Models.Core.Common |
| { |
| public abstract class ValueObject |
| { |
| protected abstract IEnumerable<object> GetEqualityComponents(); |
| public override bool Equals(object obj) |
| { |
| if (obj == null) |
| return false; |
| |
| if (GetType() != obj.GetType()) |
| return false; |
| |
| var valueObject = (ValueObject)obj; |
| |
| return GetEqualityComponents().SequenceEqual(valueObject.GetEqualityComponents()); |
| } |
| |
| public override int GetHashCode() |
| { |
| return GetEqualityComponents() |
| .Aggregate(1, (current, obj) => |
| { |
| unchecked |
| { |
| return current * 23 + (obj?.GetHashCode() ?? 0); |
| } |
| }); |
| } |
| |
| public static bool operator ==(ValueObject a, ValueObject b) |
| { |
| if (a is null && b is null) |
| return true; |
| |
| if (a is null || b is null) |
| return false; |
| |
| return a.Equals(b); |
| } |
| |
| public static bool operator !=(ValueObject a, ValueObject b) |
| { |
| return !(a == b); |
| } |
| } |
| } |