欢迎使用SKU lib
理论知识
http://blog.csdn.net/doubleping/article/details/78502514
git 地址
https://github.com/httpping/skulib/tree/dev
本LIb 使用[Java annotation][6] 技术实现对 entity的解析
主要的annotation 组成class
- SKUCount
- SKUId
- SKUKey
- SkuAnalysis
代码
KUCount 标记sku 对应的数量
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SKUCount {
}
SKUId 标记sku 对应的id
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SKUId {
}
SKUKey 标记sku 对应的 属性 比如颜色 name() 表示属性的对应group 默认为属性名
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface SKUKey {
String name() default "";
}
使用 注解 demo
public class SkuEntity implements Serializable {
@SKUKey(name = "color23")
public String color ="红";
@SKUKey
public String size ="L";
@SKUKey
public String neicun ="2G";
@SKUId
public String id = "123";
@SKUCount
public int count =10;
....get
}
分析 annotation 工具
/**
* analysis
*
* @author tanping
* @create 2017-11-28 9:59
**/
public class SkuAnalysis {
/**
* 倒排索引 key 和 index 文档
*/
HashMap<String,List<String>> keyIndex = new HashMap<>();
/**
* key 组 将所有生产的索引找到组
*
* 例子: 红 : color
* L : size
*/
Map<String,String> keyGroup = new HashMap<>();
/**
* SKU 文档数量
*/
Map<String,Integer> docCount = new HashMap<>();
/**
* id key 索引
*/
HashMap<String,List<String>> idKeys = new HashMap<>();
public static <T> SkuAnalysis analysis(List<T> skuEntity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
SkuAnalysis skuAnalysis = new SkuAnalysis();
for (T entity: skuEntity){
skuAnalysis.parse(entity);
}
return skuAnalysis;
}
public static <T> SkuAnalysis analysis(T skuEntity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
SkuAnalysis skuAnalysis = new SkuAnalysis();
skuAnalysis.parse(skuEntity);
return skuAnalysis;
}
/**
* 解析
* @param entity
*/
private <T> void parse(T entity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Class cl = entity.getClass();
String id = getId(entity);
int count = getCount(entity);
if (id == null || "".equals(id)){// 垃圾数据
return;
}
docCount.put(id,count);
queryKey(entity,id,count);
}
/**
* 查询key
* @param entity
* @param id
* @param <T>
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
private <T> void queryKey(T entity,String id,int count) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class cl = entity.getClass();
for (Field field : cl.getFields()){
Annotation[] annotations = PropertyUtils.getFieldAnnotations(cl, field.getName());
if (annotations !=null){
for (Annotation annotation : annotations){
if (annotation.annotationType().equals(SKUKey.class)){
String key = (String) PropertyUtils.invokeGetter(entity,field.getName());
List<String> values = keyIndex.get(key);
if (values == null){
values = new ArrayList<>();
}
if (count !=0) {//0的不需要放在文档中,优化过滤规则
values.add(id);
}
keyIndex.put(key,values);
Method name = annotation.getClass().getDeclaredMethod("name");
String group = (String) name.invoke(annotation);
if (group ==null || "".equals(group)){
group = field.getName(); //名称作为组
}
//根据key 找到对应的组
keyGroup.put(key,group);
//id key
values = idKeys.get(id);
if (values == null){
values = new ArrayList<>();
}
values.add(key);
idKeys.put(id,values);
}
}
}
}
}
/**
* 获取SKU ID
* @param entity
* @param <T>
* @return
*/
private <T> String getId(T entity){
Class cl = entity.getClass();
String id = null;
for (Field field : cl.getFields()){
Annotation[] annotations = PropertyUtils.getFieldAnnotations(cl, field.getName());
if (annotations !=null){
for (Annotation annotation : annotations){
if (annotation.annotationType().equals(SKUId.class)){
id = (String) PropertyUtils.invokeGetter(entity,field.getName());
return id;
}
}
}
}
return null;
}
/**
* 获取SKU count数量
* @param entity
* @param <T>
* @return
*/
private <T> int getCount(T entity){
Class cl = entity.getClass();
int count = 0;
for (Field field : cl.getFields()){
Annotation[] annotations = PropertyUtils.getFieldAnnotations(cl, field.getName());
if (annotations !=null){
for (Annotation annotation : annotations){
if (annotation.annotationType().equals(SKUCount.class)){
count = (int) PropertyUtils.invokeGetter(entity,field.getName());
return count;
}
}
}
}
return count;
}
/**
* 根据关键字过滤 SKU
* @param keys
* @return
*/
public List<String> querySkuId(List<String> keys){
List<String> result = null;
for (String key : keys) {
List indexs = keyIndex.get(key);
if (indexs == null){ //没有找到代表无结果
return null;
}
if (result == null){
result = indexs;
}else {
result.retainAll(indexs);
}
}
return result;
}
/**
* 根据关键字过滤 SKU
* @param keys
* @return
*/
public List<String> querySkuKeys(List<String> keys){
List<String> result = querySkuId(keys);
result = getKeys(result);
return result;
}
/**
* 根据id获取所有的id
* @param ids
* @return
*/
public List<String> getKeys(List<String> ids){
List<String> result = new ArrayList<>();
for (String id: ids){
List<String> values = idKeys.get(id);
result.addAll(values);
}
return result;
}
public HashMap<String, List<String>> getKeyIndex() {
return keyIndex;
}
public Map<String, String> getKeyGroup() {
return keyGroup;
}
public Map<String, Integer> getDocCount() {
return docCount;
}
}
SkuAnalysis 主要解析对应的 entity 的实体,得到一组倒排索引和 关键字等数据
以上条件准备好了后 来看demo
/**
* @author tanping
* @create 2017-11-28 10:37
**/
public class TestDemo {
public static void main(String[] args){
System.out.println("解析");
List<SkuEntity> skuEntities = new ArrayList<>();
SkuEntity entity = new SkuEntity();
skuEntities.add(entity);
entity = new SkuEntity();
entity.setId("31");
entity.setColor("白色");
entity.setCount(43);
skuEntities.add(entity);
entity = new SkuEntity();
entity.setId("33");
entity.setColor("白色");
entity.setNeicun("8g");
entity.setCount(9);
skuEntities.add(entity);
SkuAnalysis analysis = null;
try {
analysis = SkuAnalysis.analysis(skuEntities);
System.out.println(analysis.getDocCount());
System.out.println(analysis.getKeyGroup());
System.out.println(analysis.getKeyIndex());
} catch (Exception e) {
e.printStackTrace();
}
List<String > select = new ArrayList<>();
select.add("红");
// select.add("8g");
List result = analysis.querySkuId(select);
System.out.println("result : " + result);
result = analysis.querySkuKeys(select);
System.out.println("result : " + result);
}
}
执行结果 解析
getDocCount : {33=9, 123=10, 31=43}
getKeyGroup : {红=color23, 2G=2g, 白色=color23, L=size, 8g=2g}
getKeyIndex : {红=[123], 2G=[123, 31], 白色=[31, 33], L=[123, 31, 33], 8g=[33]}
querySkuId : result : [123]
querySkuKeys : result : [红, L, 2G]
Process finished with exit code 0