Java注解

  • Post author:
  • Post category:java


1. 定义注解

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Persistent {
	enum FilterType {
		Numeric, String
	};

	enum ResType {
		Rat, Cat, RatCat, None
	}

	short index() default 0;

	String id() default "";

	String value() default "";

	FilterType filterType() default FilterType.Numeric;

	boolean isVisible() default true;

	ResType resType() default ResType.None;
}

2. 定义JavaBean

public class Tuple<T, V> implements ITuple<T, V> {

	private T key;
	private V value;

	@Override
	public T getKey() {
		return key;
	}

	@Override
	public void setKey(T key) {
		this.key = key;
	}

	@Override
	public V getValue() {
		return value;
	}

	@Override
	public void setValue(V value) {
		this.value = value;
	}

}

public class Column<T, V> extends Tuple<T, V> implements IColumn<T, V> {

	private byte filterType;
	private boolean isVisible;

	@Override
	public byte getFilterType() {
		return filterType;
	}

	@Override
	public void setFilterType(byte filterType) {
		this.filterType = filterType;
	}

	@Override
	public boolean isVisible() {
		return this.isVisible;
	}

	@Override
	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

}

public class Row<T, V, K extends ITuple<T, V>> implements IRow<T, V, K> {

	private List<K> columns;

	@Override
	public List<K> getColumns() {
		return columns;
	}

	@Override
	public void setColumns(List<K> columns) {
		this.columns = columns;
	}

}

public class User implements IUser {

	@Persistent(index = 0, id = "name", filterType = FilterType.String)
	private String name;

	@Persistent(index = 3, id = "age", isVisible = false)
	private short age;

	@Persistent(index = 2)
	private byte gendar;

	public byte getGendar() {
		return gendar;
	}

	public void setGendar(byte gendar) {
		this.gendar = gendar;
	}

	public short getAge() {
		return age;
	}

	public void setAge(short age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

3. Demo

public class Main {

	public static void main(String[] args) {
		List<LoginUser> users = new ArrayList<LoginUser>();
		LoginUser user = new LoginUser();
		user.setAge((short) 26);
		user.setName("9528");
		user.setGendar((byte) 0);
		user.setLoginTime(new Date());
		users.add(user);

		user = new LoginUser();
		user.setAge((short) 27);
		user.setName("9527");
		user.setGendar((byte) 1);
		user.setLoginTime(new Date());
		users.add(user);

		WebUser webUser = getWebUser(users);

		List<? extends IColumn<String, String>> columns = webUser.getColumns();

		List<? extends IRow<String, String, ? extends ITuple<String, String>>> rows = webUser.getRows();

		for (IColumn<String, String> column : columns) {
			System.out.print(column.getKey());
			System.out.print(":");
			System.out.print(column.getValue());
			System.out.print("#");
		}

		System.out.println();

		for (IRow<String, String, ? extends ITuple<String, String>> row : rows) {
			List<? extends ITuple<String, String>> cols = row.getColumns();
			for (ITuple<String, String> col : cols) {
				System.out.print(col.getKey());
				System.out.print(":");
				System.out.print(col.getValue());
				System.out.print("#");
			}
			System.out.println();
		}

		System.out.println("---------");
	}

	private static WebUser getWebUser(List<LoginUser> users) {
		WebUser result = new WebUser();

		LoginUser first = users.get(0);

		List<Column<String, String>> columns = getColumns(first);

		result.setColumns(columns);

		List<Row<String, String, Tuple<String, String>>> rows = new ArrayList<Row<String, String, Tuple<String, String>>>();

		for (LoginUser user : users) {
			Row<String, String, Tuple<String, String>> row = new Row<String, String, Tuple<String, String>>();
			List<Tuple<String, String>> cols = getCols(user);
			row.setColumns(cols);
			rows.add(row);
		}

		result.setRows(rows);

		return result;
	}

	private static List<Field> getFields(Class<?> type) {
		List<Field> result = new ArrayList<Field>();
		for (Field field : type.getDeclaredFields()) {
			result.add(field);
		}
		if (type.getSuperclass().getDeclaredFields().length > 0) {
			result.addAll(getFields(type.getSuperclass()));
		}
		return result;
	}

	private static List<Tuple<String, String>> getCols(LoginUser user) {
		List<PersistentItem<String, String>> result = new ArrayList<PersistentItem<String, String>>();

		for (Field field : getFields(LoginUser.class)) {
			Persistent persistent = field.getAnnotation(Persistent.class);
			if (persistent != null) {
				PersistentItem<String, String> item = new PersistentItem<String, String>();
				item.setKey(getPersistentId(persistent, field));
				field.setAccessible(true);
				String value = null;
				try {
					value = String.valueOf(field.get(user));
					field.setAccessible(false);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				item.setValue(value);
				item.setIndex(persistent.index());
				result.add(item);
			}
		}

		sortItems(result);

		return convert(result);
	}

	private static List<Column<String, String>> getColumns(LoginUser first) {
		List<PersistentItem<String, String>> result = new ArrayList<PersistentItem<String, String>>();

		for (Field field : getFields(LoginUser.class)) {
			Persistent persistent = field.getAnnotation(Persistent.class);
			if (persistent != null) {
				PersistentItem<String, String> item = new PersistentItem<String, String>();

				item.setFilterType((byte) persistent.filterType().ordinal());
				item.setKey(getPersistentId(persistent, field));
				item.setValue(getResValue(persistent.id()));
				item.setVisible(persistent.isVisible());
				item.setIndex(persistent.index());

				result.add(item);
			}
		}
		sortItems(result);

		return convert(result);
	}

	private static String getPersistentId(Persistent persistent, Field field) {
		String result = persistent.id();
		if (result == null || result.isEmpty()) {
			result = field.getName();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	private static <T extends Tuple<String, String>> List<T> convert(List<PersistentItem<String, String>> persistents) {
		return (List<T>) persistents;
	}

	private static void sortItems(List<PersistentItem<String, String>> result) {
		Collections.sort(result, new Comparator<PersistentItem<String, String>>() {

			@Override
			public int compare(PersistentItem<String, String> arg0, PersistentItem<String, String> arg1) {
				return arg0.getIndex() - arg1.getIndex();
			}
		});

	}

	private static String getResValue(String id) {
		return id;
	}

}



版权声明:本文为lsj308原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。