All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.liuanxin.api.model.Recursive Maven / Gradle / Ivy

The newest version!
package com.github.liuanxin.api.model;

import com.github.liuanxin.api.constant.ApiConst;
import com.github.liuanxin.api.util.Tools;

import java.util.Collection;
import java.util.Map;

public class Recursive {

    private Recursive parent;

    private String fieldName;
    private Class self;


    public Recursive() {}
    public Recursive(Recursive parent, String fieldName, Class self) {
        this.parent = parent;
        this.fieldName = fieldName;
        this.self = self;
    }
    /**
     * use with field: new Recursive(parent, fieldName, field.getGenericType().toString())
     *
     * @param parent parent
     * @param fieldName field name
     * @param genericType generic type
     */
    public Recursive(Recursive parent, String fieldName, String genericType) {
        this.parent = parent;
        this.fieldName = fieldName;
        this.self = getGenericType(genericType);
    }


    public Recursive getParent() {
        return parent;
    }
    public Recursive setParent(Recursive parent) {
        this.parent = parent;
        return this;
    }

    public String getFieldName() {
        return fieldName;
    }
    public Recursive setFieldName(String fieldName) {
        this.fieldName = fieldName;
        return this;
    }

    public Class getSelf() {
        return self;
    }
    public Recursive setSelf(Class self) {
        this.self = self;
        return this;
    }


    /**
     * 
     * public class A { ... private A x; }
     *
     * OR
     *
     * public class A { ... private List<A> x; }
     *
     * OR
     *
     * public class A { ... private Map<String, A> x; }
     *
     * OR
     *
     * public class A { ... private B x; }
     * public class B { ... private A xx; }
     *
     * OR
     *
     * public class A { ... private B x; }
     * public class B { ... private List<C> xx; }
     * public class C { ... private Map<String, A> xxx; }
     *
     * will return true
     * 
* @return recursive return true */ public boolean checkRecursive() { return check(self, parent); } private static boolean check(Class self, Recursive parent) { if (Tools.isNull(parent)) { return false; } else if (parent.self == self) { return true; } else { return check(self, parent.parent); } } /** *
     * public class A { ... private A x; }
     * will return "A --> A x"
     *
     *
     * public class A { ... private B x; }
     * public class B { ... private A xx; }
     * will return "A --> B x --> A xx"
     *
     *
     * public class A { ... private B x; }
     * public class B { ... private List<C> xx; }
     * public class C { ... private Map<String, A> xxx; }
     * will return "A --> B x --> C xx --> A xxx --> B x"
     * 
* * @return orbit */ public String getOrbit() { StringBuilder sbd = new StringBuilder(); orbit(this, sbd); return sbd.toString(); } private static void orbit(Recursive self, StringBuilder sbd) { if (Tools.isNotNull(self)) { if (Tools.isNotNull(self.parent)) { orbit(self.parent, sbd); sbd.append(" --> "); } sbd.append(self.self.getName()); if (Tools.isNotNull(self.fieldName)) { sbd.append(ApiConst.SPACE).append(self.fieldName); } } } private static Class getGenericType(String str) { if (str.contains("<") && str.contains(">")) { Class parent = getClass(str.substring(0, str.indexOf("<"))); if (parent == null) { return getClass(str); } else { // List or Map String genericString = str.substring(str.indexOf("<") + 1, str.lastIndexOf(">")); if (Collection.class.isAssignableFrom(parent)) { return getGenericType(genericString.trim()); } else if (Map.class.isAssignableFrom(parent)) { // Map use Value, ignore key String[] keyValue = genericString.split(","); if (keyValue.length == 2) { return getGenericType(keyValue[1].trim()); } } return parent; } } else { return getClass(str); } } private static Class getClass(String clazz) { try { String prefix = "class "; return Class.forName(clazz.startsWith(prefix) ? clazz.substring(prefix.length()) : clazz); } catch (ClassNotFoundException ignore) { return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy