/** 解析数据行 */ private static boolean parserDate(String line) { if (line == null) { return true; } Item item = new Item(); Attribute attr = null; int index = 0; int si; String data; int nData; /* * 根据解析出来的属性的个数和类别,解析数据。 */ for (AttributeClass ac : arff.aclasses) { si = index; while (index < line.length() && line.charAt(index) != ',') ++index; data = line.substring(si, index); if (ac.type == AttributeType.NUME) { nData = Integer.valueOf(data); attr = new Attribute(ac, nData); } else { attr = new Attribute(ac, ac.getCateIndex(data)); } item.addAttr(attr); ++index; } arff.addItem(item); return true; }
/** * Helper method for subclasses to implement getFooAttribute(String attributeName), as such: * * <pre> * public class User * { * private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>(); * * public String getUserAttribute(String attributeName) * { * return PersistentAttribute.getAttribute(userAttributes, attributeName); * } * } * </pre> * * @param attributeSet the set of objects derived from PersistentAttribute to turn into a * Map<String,String> * @param attributeName the name of the desired attribute * @return the attribute value or null if the attribute does not exist */ public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>> String getAttribute(Set<AttributeClass> attributeSet, String attributeName) { for (AttributeClass attribute : attributeSet) { if (attribute.getName().equals(attributeName)) return attribute.getValue(); } return null; }
/** * Helper method for subclasses to implement getFooAttributes(), as such: * * <pre> * public class User * { * private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>(); * * public Map<String,String> getUserAttributes() * { * return PersistentAttribute.buildMap(userAttributes); * } * } * </pre> * * @param attributeSet the set of objects derived from PersistentAttribute to turn into a * Map<String,String> * @return an unmodifiable Map representing the attributes attached to the object */ public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>> Map<String, String> buildMap(Set<AttributeClass> attributeSet) { Map<String, String> map = new HashMap<String, String>(attributeSet.size()); for (AttributeClass attribute : attributeSet) { map.put(attribute.getName(), attribute.getValue()); } return Collections.unmodifiableMap(map); }
/** * Helper method for subclasses to implement removeFooAttribute(), as such: * * <pre> * public class User * { * private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>(); * * public void removeUserAttribute(String name) * { * PersistentAttribute.clearAttribute(userAttributes, name); * } * } * </pre> * * @param attributeSet the set to modify by removing the target attribute * @param name the attribute name */ public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>> void clearAttribute(Set<AttributeClass> attributeSet, String name) { validateNotNull(attributeSet, "clearExistingAttribute.attributeSet must be non-null"); validateNotNull(name, "clearExistingAttribute.name must be non-null"); for (AttributeClass existingAttribute : attributeSet) { if (existingAttribute.getName().equals(name)) { attributeSet.remove(existingAttribute); existingAttribute.setOwner(null); return; } } }
/** 解析属性行 */ private static boolean parserAttribute(String line) { // @attribute sex {Female,Male} // @attribute capital-gain numeric if (line == null) { return true; } int index = 0; // 字符串的当前位置。 // 跳过"@attribute" index += "@attribute".length(); // 跳过空白。 while (Character.isWhitespace(line.charAt(index))) ++index; // 遇到了属性的名字 String name; int nameStart = index; // 名字开始的位置。 while (!Character.isWhitespace(line.charAt(index))) ++index; // 此时index直向属性名字后面的空格。 name = line.substring(nameStart, index); // 下面解析属性值。 // 跳过空白。 while (Character.isWhitespace(line.charAt(index))) ++index; if (line.charAt(index) == '{') { // 是categorical类型属性 // @attribute sex {Female,Male} AttributeClass ac = new AttributeClass(AttributeType.CATE, name); String cateName; int cateStart; while (line.charAt(index) != '}') { ++index; cateStart = index; while (line.charAt(index) != ',' && line.charAt(index) != '}') { ++index; } cateName = line.substring(cateStart, index); ac.addCate(cateName); } arff.addAttrClass(ac); } else { // 是numeric类型属性 arff.addAttrClass(new AttributeClass(AttributeType.NUME, name)); } return true; }
/** * Helper method for subclasses to implement setFooAttribute(), as such: * * <pre> * public class User * { * private final Set<UserAttribute> userAttributes = new HashSet<UserAttribute>(); * * public void setUserAttribute(String name, String value) * { * PersistentAttribute.setAttribute(this, userAttributes, name, value, UserAttribute.class); * } * } * </pre> * * This function will overwrite any existing attribute of the same name. * * @param owner the owner of the attribute collection (usually the <code>this</code> pointer of * the caller * @param attributeSet the set to modify by adding or replacing the target attribute * @param name the attribute name * @param value the attribute value * @param attributeClass the Class of the PersistentAttribute subclass * @return the newly created attribute object (already stored in the attributeSet) */ public static <OwnerClass, AttributeClass extends PersistentAttribute<OwnerClass>> AttributeClass setAttribute( OwnerClass owner, Set<AttributeClass> attributeSet, String name, String value, Class<? extends AttributeClass> attributeClass) { validateNotNull(attributeSet, "setAttribute.attributeSet must be non-null"); validateNotNull(name, "clearExistingAttribute.name must be non-null"); validateNotNull(value, "clearExistingAttribute.value must be non-null"); validateNotNull(attributeClass, "clearExistingAttribute.attributeClass must be non-null"); for (AttributeClass existingAttribute : attributeSet) { if (existingAttribute.getName().equals(name)) { // we must remove/re-add the existing attribute when changing its // value so that it gets filed in the correct hash bucket attributeSet.remove(existingAttribute); existingAttribute.setValue(value); attributeSet.add(existingAttribute); return existingAttribute; } } AttributeClass attribute; try { Constructor<? extends AttributeClass> constructor = attributeClass.getConstructor(String.class, String.class); attribute = constructor.newInstance(name, value); } catch (Exception e) { throw new IllegalArgumentException("can't instantiate class: " + attributeClass.getName(), e); } attributeSet.add(attribute); attribute.setOwner(owner); return attribute; }