<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
CompositeKeyGenerator.java
package com.blogspot.na5cent.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
/**
*
* @author redcrow
*/
public class CompositeKeyGenerator {
private static final String KEY_PATTERN = "\\{\\s*(.*?)\\s*\\}";
private static final String KEY_SPLITOR = ":";
public static String generateKey(String prefixName, String... keys) {
return prefixName + "{" + StringUtils.join(keys, KEY_SPLITOR) + "}";
}
private static String selectKey(String text) {
Pattern patternCompile = Pattern.compile(KEY_PATTERN);
Matcher matcher = patternCompile.matcher(text);
if (!matcher.find()) {
return text;
}
return matcher.group(1);
}
public static String getKeyIndex(String key, int index) {
return StringUtils.split(selectKey(key), KEY_SPLITOR)[index];
}
}
CompositeKeyGeneratorTest.java
package com.blogspot.na5cent.util.test;
import org.junit.Test;
import static org.junit.Assert.*;
import com.blogspot.na5cent.util.CompositeKeyGenerator;
/**
*
* @author redcrow
*/
public class CompositeKeyGeneratorTest {
@Test
public void test() {
String KEY = "PersonnelInfo{10666:1.2.3.4}";
assertEquals(KEY, CompositeKeyGenerator.generateKey("PersonnelInfo", "10666", "1.2.3.4"));
assertEquals("10666", CompositeKeyGenerator.getKeyIndex(KEY, 0));
assertEquals("1.2.3.4", CompositeKeyGenerator.getKeyIndex(KEY, 1));
}
}
Example to use
ServiceInfoPK.java
package com.blogspot.na5cent.model.pk;
import java.io.Serializable;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import com.blogspot.na5cent.util.CompositeKeyGenerator;
/**
*
* @author redcrow
*/
@Embeddable
public class ServiceInfoPK implements Serializable {
@Column(name = "hcode")
private String hcode;
@Column(name = "service_id")
private String serviceId;
public ServiceInfoPK() {
}
public ServiceInfoPK(String pk) {
this.hcode = CompositeKeyGenerator.getKeyIndex(pk, 0);
this.serviceId = CompositeKeyGenerator.getKeyIndex(pk, 1);
}
public ServiceInfoPK(String hcode, String serviceId) {
this.hcode = hcode;
this.serviceId = serviceId;
}
public String getHcode() {
return hcode;
}
public void setHcode(String hcode) {
this.hcode = hcode;
}
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
@Override
public int hashCode() {
int hash = 7;
hash = 41 * hash + Objects.hashCode(this.hcode);
hash = 41 * hash + Objects.hashCode(this.serviceId);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ServiceInfoPK other = (ServiceInfoPK) obj;
if (!Objects.equals(this.hcode, other.hcode)) {
return false;
}
if (!Objects.equals(this.serviceId, other.serviceId)) {
return false;
}
return true;
}
@Override
public String toString() {
return CompositeKeyGenerator.generateKey("ServiceInfo", hcode, serviceId);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น