-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathNamed.java
More file actions
80 lines (71 loc) · 2.18 KB
/
Named.java
File metadata and controls
80 lines (71 loc) · 2.18 KB
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
/********************************************************************************
* Copyright (c) 2019 [Open Lowcode SAS](https://openlowcode.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
********************************************************************************/
package org.openlowcode.tools.misc;
/**
* A named object is an object that can be identified, in the relevant
* namespace, by a unique String
*
* @author <a href="https://openlowcode.com/" rel="nofollow">Open Lowcode SAS</a>
*
*/
public abstract class Named implements NamedInterface {
private String name;
@Override
public String getName() {
return this.name;
}
/**
* @param name a name, that will be cleaned to become the unique identifier of
* the object
*/
public Named(String name) {
this.name = cleanName(name);
}
/**
* changes the name of the object. Note that changing the name after the object
* has been inserted in a NamedList
*
* @param name
*/
public void changeName(String name) {
this.name = cleanName(name);
}
/**
* This method aims at cleaning a String used for name identification. In
* current inplementation, it will keep only characters, numbers and underscore
*
* @param name the input string
* @return
*/
public static String cleanName(String name) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < name.length(); i++) {
char currentchar = name.charAt(i);
if (Character.isLetterOrDigit(currentchar))
result.append(currentchar);
if (currentchar == '_')
result.append(currentchar);
}
return result.toString().toUpperCase();
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!obj.getClass().equals(this.getClass()))
return false;
Named namedobject = (Named) obj;
return (namedobject.name.equals(this.name));
}
@Override
public int hashCode() {
return (this.getClass().getName()+"-"+this.name).hashCode();
}
}