This repository has been archived by the owner on May 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 789
/
Connection.java
156 lines (137 loc) · 5.54 KB
/
Connection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Copyright (c) 1997, 2015 by ProSyst Software GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.smarthome.automation.core.internal;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.smarthome.automation.Module;
import org.eclipse.smarthome.automation.Rule;
import org.eclipse.smarthome.automation.type.Input;
import org.eclipse.smarthome.automation.type.Output;
import org.slf4j.Logger;
/**
* This class defines connection between {@link Input} of the current {@link Module} and {@link Output} of the external
* one. The current module is the module containing {@link Connection} instance and the external one is the
* module where the current is connected to. <br>
* The input of the current module is defined by name of the {@link Input}. The {@link Output} of the external module is
* defined by id of the module and name of the output.
*
* @author Yordan Mihaylov - Initial Contribution
*/
public class Connection {
public static final String REF_IDENTIFIER = "$";
private String ouputModuleId;
private String outputName;
private String inputName;
/**
* This constructor is responsible for creation of connections between modules in the rule.
*
* @param inputName is an unique name of the {@code Input} in scope of the {@link Module}.
* @param ouputModuleId is an unique id of the {@code Module} in scope of the {@link Rule}.
* @param outputName is an unique name of the {@code Output} in scope of the {@link Module}.
*/
public Connection(String inputName, String ouputModuleId, String outputName) {
validate("inputName", inputName);
validate("outputName", outputName);
this.inputName = inputName;
this.ouputModuleId = ouputModuleId;
this.outputName = outputName;
}
/**
* This method is used to get id of external {@link Module} of this
* connection.
*
* @return id of external {@link Module}
*/
public String getOuputModuleId() {
return ouputModuleId;
}
/**
* This method is used to get output name of external {@link Module} of this
* connection.
*
* @return name of {@link Output} of external {@link Module}
*/
public String getOutputName() {
return outputName;
}
/**
* This method is used to get input name of current {@link Module} of this
* connection.
*
* @return name {@link Input} of the current {@link Module}
*/
public String getInputName() {
return inputName;
}
/**
* Compare two connection objects.
*/
@Override
public boolean equals(Object obj) {
return (inputName != null && obj instanceof Connection) && inputName.equals(((Connection) obj).getInputName());
};
@Override
public int hashCode() {
return inputName.hashCode();
};
/**
* This method is used to validate the connection.
*
* @param field serves to construct an understandable message that indicates what property of the connection is not
* correct.
* @param id is the value of the specified property. It can't be empty string.
*/
private void validate(String field, String id) {
if (id == null || id.length() == 0) {
throw new IllegalArgumentException("Invalid identifier for " + field);
}
}
@Override
public String toString() {
return "Connection " + ouputModuleId + "." + outputName + "->" + inputName;
}
/**
* This method is used for collecting of Connections of {@link Module}s.
*
* @param type specifies the type of the automation object - module type, rule or rule template.
* @param UID is the unique identifier of the automation object - module type, rule or rule template.
* @param jsonModule is a JSONObject representing the module.
* @param exceptions is a list used for collecting the exceptions occurred during {@link Module}s creation.
* @param log is used for logging of exceptions.
* @return collected Connections
*/
public static Set<Connection> getConnections(Map<String, String> inputs, Logger log) {
Set<Connection> connections = new HashSet<Connection>(11);
if (inputs != null) {
for (Entry<String, String> input : inputs.entrySet()) {
String inputName = input.getKey();
String outputName = null;
String output = input.getValue();
if (output.startsWith(REF_IDENTIFIER)) {
outputName = output;
Connection connection = new Connection(inputName, null, outputName);
connections.add(connection);
} else {
int index = output.indexOf('.');
if (index != -1) {
String outputId = output.substring(0, index);
outputName = output.substring(index + 1);
Connection connection = new Connection(inputName, outputId, outputName);
connections.add(connection);
} else {
log.error("Wrong format of Output : {}: {}", inputName, output);
continue;
}
}
}
}
return connections;
}
}