1、15161718192021222324package com.stackhunter.util.tostring.example;import com.stackhunter.example.employee.Department;import com.stackhunter.example.employee.Employee;import com.stackhunter.example.employee.Manager;import com.stackhunter.example.people.Person;import com.stackhunter.util.tostring.Stri
2、ngGenerator;public class StringGeneratorExample public static void main(String args) Department department = new Department(5775, Sales).setEmployees(new Employee(111, Bill, Gates), new Employee(222, HowardSchultznew Manager(333, JeffBezos, 75000);System.out.println(StringGenerator.generate(departme
3、nt);System.out.println(StringGenerator.generate(new int 111, 222, 333 );System.out.println(StringGenerator.generate(true);StringGenerator.generate()将department,数组和boolean值进行格式化输出。com.stackhunter.example.employee.Department129719f4deptId = 5775employeeList = java.util.ArrayList7037717aemployeeList0 =
4、 com.stackhunter.example.employee.Employee17a323c0firstName = Billid = 111lastName = GatesemployeeList1 = com.stackhunter.example.employee.Employee57801e5ffirstName = Howardid = 222lastName = SchultzemployeeList2 = com.stackhunter.example.employee.Manager1c4a1bdabudget = 75000.0firstName = Jeffid =
5、333lastName = Bezosname = SalesI39df3255object0 = 111object1 = 222object2 = 333true对象迭代器ObjectIterator使用迭代器模式遍历对象的属性,以键值对形式保存。对象中的Java bean、集合、数组及map都要进行迭代。ObjectIterator也会考虑到对象之间循环引用的处理。import com.stackhunter.util.objectiterator.ObjectIterator;public class ObjectIteratorExample ObjectIterator itera
6、tor = new ObjectIterator(some department, department);while (iterator.next() System.out.println(iterator.getName() + = + iterator.getValueAsString();通过遍历整个对象生成键值对的集合。使用getValueAsString()方法而不是toString()格式化输出。对于原始类型、包装类型、字符串、日期和枚举使用原始的toString()实现。对于其他类型输出类名和hash值。ObjectIterator.getDepth()会增加缩进,输出更易读。
7、调用next()之前使用nextParent()缩短当前分支跳跃到下一属性。some department=com.stackhunter.example.employee.Department780324ffdeptId=5775employeeList=java.util.ArrayList6bd15108employeeList0=com.stackhunter.example.employee.Employee22a79c31firstName=Bill.Java对象迭代器的具体实现实现iterator模式的第一步是创建通用的迭代器接口:IObjectIterator。无论遍历的对象是
8、Java bean、数组还是map都可以使用该接口。public interface IObjectIterator boolean next();String getName();Object getValue();使用该接口可以按照单一顺序依次获取当前属性的name和value。实现了IObjectIterator的类用来处理某一种类型的对象。大多数类调用getName()返回名称前缀。ArrayIterator使用了元素的索引:return name + + nextIndex + ;。属性迭代器PropertyIterator可能是最重要的迭代类。它使用Java bean intros
9、pection读取对象属性,将它们转化为键值对序列。2526272829303132333435363738394041424344454647484950515253545556public class PropertyIterator implements IObjectIterator private final Object object;private final PropertyDescriptor properties;private int nextIndex = -1;private PropertyDescriptor currentProperty;public Prop
10、ertyIterator(Object object) this.object = object;try BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass();properties = beanInfo.getPropertyDescriptors(); catch (RuntimeException e) throw e; catch (Exception e) throw new RuntimeException(e.getMessage(), e);Overridepublic boolean next() if (
11、nextIndex + 1 = properties.length) return false;nextIndex+;currentProperty = propertiesnextIndex;if (currentProperty.getReadMethod() = null | class.equals(currentProperty.getName() return next();return true;public String getName() if (currentProperty = null) return null;return currentProperty.getNam
12、e();public Object getValue() return currentProperty.getReadMethod().invoke(object);数组迭代器ArrayIterator通过反射得到数组的长度,进而检索每个数据元素。ArrayIterator不关心从getValue()方法返回值的具体细节。它们一般情况下被传递给PropertyIterator。public class ArrayIterator implements IObjectIterator private final String name;private final Object array;pri
13、vate final int length;private Object currentElement;public ArrayIterator(String name, Object array) this.name = name;this.array = array;this.length = Array.getLength(array);= length) currentElement = Array.get(array, nextIndex);return currentElement;集合迭代器CollectionIterator与ArrayIterator非常相似。使用java.l
14、ang.Iterable调用它的Iterable.iterator()方法初始化内部迭代器。Map迭代器MapIterator遍历java.util.Map的entry。它并不深入到每个entry的键值对,这个工作由MapEntryIterator类完成。public class MapIterator implements IObjectIterator private Iterator entryIterator;private Map.Entry, ? currentEntry;public MapIterator(String name, Map map) this.entryIter
15、ator = map.entrySet().iterator();if (entryIterator.hasNext() currentEntry = (Entry) entryIterator.next();Map Entry迭代器MapEntryIterator处理java.util.Map的单个entry。它只返回两个值:entry的键和值。与ArrayIterator及其他的类似,如果是复杂类型的话,它的结果可能最终传递给PropertyIterator,作为Java bean处理。根迭代器RootIterator返回单个元素初始节点。可以把它想成XML文件的根节点。目的是发起整个遍历
16、过程。整合ObjectIterator类作为门面角色(Facade),包装了所有的遍历逻辑。它根据最后一次getValue()的返回值类型决定哪个IObjectIterator的子类需要实例化。当子迭代器在内部创建时它在栈中保存当前迭代器的状态。它也暴露了getChild()和getDepth()方法为调用者展示当前进度。private IObjectIterator iteratorFor(Object object) if (object = null) if (object.getClass().isArray() return new ArrayIterator(name, objec
17、t);if (object instanceof Iterable) return new CollectionIterator(name, (Iterable) object);if (object instanceof Map) return new MapIterator(name, (Mapif (object instanceof Map.Entry) return new MapEntryIterator(name, (Map.Entry= MAX_STRING_LENGTH) return s;if (iterator.getChild() = MAX_CHILDREN) iterator.nextParent();continue;String valueAsString = iterator.getValueAsString();s += System.lineSeparator();s += indent(iterator.
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2