Skip to content

Commit

Permalink
add support for typealias
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Feb 26, 2018
1 parent 57ab58b commit 4917528
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 334 deletions.
110 changes: 110 additions & 0 deletions processor/src/main/java/org/bsc/processor/TSType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.bsc.processor;

import java.util.HashMap;

/**
*
* @author softphone
*
*/
@SuppressWarnings("serial")
public class TSType extends HashMap<String,Object> {

protected TSType() {
super(3);
}

public static TSType from( Class<?> cl, boolean export ) {
return new TSType() {{
put( "value", cl);
put( "export", export);
}};
}

public static TSType from( Class<?> cl, String alias, boolean export ) {
return new TSType() {{
put( "value", cl);
put( "export", export);
put( "alias", alias);
}};
}

protected static TSType from( Class<?> cl ) {
return new TSType() {{ put( "value", cl); }};
}

/**
*
* @return
*/
public Class<?> getValue() {
return getClassFrom(super.get("value"));
}

/**
*
* @return
*/
public boolean isExport() {
return (boolean) super.getOrDefault("export", false);
}

/**
*
* @return
*/
public boolean hasAlias() {
final String alias = (String) super.get("alias");
return alias!=null && !alias.isEmpty();
}

/**
*
* @return
*/
public String getAlias() {
return (String) super.get("alias");
}

/**
*
* @return
*/
public final String getTypeName() {
return (hasAlias()) ? getAlias() : getValue().getName();
}

/**
*
* @return
*/
public final String getSimpleTypeName() {
return (hasAlias()) ? getAlias() : getValue().getSimpleName();
}

/**
*
* @param dt
* @return
*/
private Class<?> getClassFrom( Object dt ) {
if( dt instanceof Class ) return (Class<?>)dt;

try {
return Class.forName(dt.toString());
} catch (ClassNotFoundException e1) {
throw new RuntimeException(String.format("class not found [%s]",dt), e1);
}
}

@Override
public boolean equals(Object o) {
return getValue().equals(((TSType)o).getValue());
}

@Override
public int hashCode() {
return getValue().hashCode();
}

}
Loading

0 comments on commit 4917528

Please sign in to comment.