/** * Creates a new type with a supertype and registers it so that * {@link #getInstance(String)} will find it. * * @param supertype * @param name * @return the new type */ private static GoogleBaseAttributeType createAndRegister( GoogleBaseAttributeType supertype, String name) { GoogleBaseAttributeType type = new GoogleBaseAttributeType(supertype, name); KNOWN_TYPES.put(type.getName(), type); return type; }
/** * Creates a new type and registers it so that {@link #getInstance(String)} * will find it. * * @param name * @return the new type */ private static GoogleBaseAttributeType createAndRegister(String name) { return createAndRegister(null, name); }
/** * Checks whether the given type is the same as this type or a subtype. * * @param subtype * @return true if <code>subtype</code> is the same type or a subtype * of the current type */ public boolean isSupertypeOf(GoogleBaseAttributeType subtype) { if (this.equals(subtype)) { return true; } GoogleBaseAttributeType otherSupertype = subtype.getSupertype(); if (otherSupertype == null) { return false; } return isSupertypeOf(otherSupertype); } }
/** * Creates a {@link GoogleBaseAttribute} of type * {@link GoogleBaseAttributeType#GROUP} and initializes it using the * current state of the object. Sub-attributes of group with type * GROUP or GROUP's derived types are ignored. * * @param name attribute name * @param group attribute value * @return a new {@link com.google.api.gbase.client.GoogleBaseAttribute} */ public static GoogleBaseAttribute createAttribute(String name, Group group) { GoogleBaseAttribute attribute = new GoogleBaseAttribute(name, GoogleBaseAttributeType.GROUP); Multimap<String, GoogleBaseAttribute> subAttributes = group.getAllSubAttributes(); for (GoogleBaseAttribute attr : subAttributes.values()) { GoogleBaseAttributeType type = attr.getType(); if (!GoogleBaseAttributeType.GROUP.equals(type) && !GoogleBaseAttributeType.GROUP.equals(type.getSupertype()) && (attr.hasValue() || attr.hasSubElements())) { attribute.addSubAttribute(attr); } } return attribute; }
@Override public boolean equals(Object o) { if (!(o instanceof GoogleBaseAttributeId)) { return false; } GoogleBaseAttributeId other = (GoogleBaseAttributeId)o; if (!name.equals(other.name)) { return false; } if (type == null) { return other.type == null; } else { return type.equals(other.type); } } }
/** * Given an XML element local name and its attributes, creates * an {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * This method reads the 'type' attribute and set it in the * {@link GoogleBaseAttribute}. * * @param attributeName attribute name, with spaces * @param xmlAttributes XML attributes for the current element * @return the GoogleBaseAttribute for the XML tag */ private GoogleBaseAttribute createExtensionAttribute( String attributeName, Attributes xmlAttributes) { String type = xmlAttributes.getValue("type"); String access = xmlAttributes.getValue("access"); boolean privateAccess = "private".equals(access); return new GoogleBaseAttribute(attributeName, GoogleBaseAttributeType.getInstance(type), privateAccess); }
private boolean hasNameAndType(GoogleBaseAttribute attr, String name, GoogleBaseAttributeType type) { GoogleBaseAttributeType subtype = attr.getAttributeId().getType(); return name.equals(attr.getAttributeId().getName()) && (type == null || subtype != null && type.isSupertypeOf(subtype)); }
/** * Returns the XML attributes that should be set to the * element corresponding to the given * {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * This method is meant to be implemented in subclasses that need * so set attributes. By default, it defines no attributes and * simply returns null. * * @param attribute * @return a collection of XML attributes or null */ private Collection<XmlWriter.Attribute> getXmlAttributes( GoogleBaseAttribute attribute) { GoogleBaseAttributeType type = attribute.getAttributeId().getType(); if (type == null) { return null; } List<XmlWriter.Attribute> attributes = new ArrayList<XmlWriter.Attribute>(); attributes.add(new XmlWriter.Attribute("type", type.toString())); if (attribute.isPrivate()) { attributes.add(new XmlWriter.Attribute("access", "private")); } return attributes; }
@Override public int hashCode() { int retval = 19 + name.hashCode(); if (type != null) { retval = retval * 37 + type.hashCode(); } return retval; }
/** * Get a type instance. * * @param typeName name of a type found in a feed. Current version of * the API will always return one of the types listed in this class * but future versions might support new types, so <code>getInstance</code> * accepts any type name * @return an instance of GoogleBaseAttributeType, or <code>null</code> * if <code>typeName</code> was null */ public static GoogleBaseAttributeType getInstance(String typeName) { if (typeName == null) { return null; } GoogleBaseAttributeType commonType = KNOWN_TYPES.get(typeName); if (commonType != null) { return commonType; } else { return new GoogleBaseAttributeType(null, typeName); } }
/** Generate the XML representation for this tag. */ public void generate(XmlWriter w, ExtensionProfile extProfile) throws IOException { if (attributes.isEmpty()) { // Nothing to write return; } w.startElement(GoogleBaseNamespaces.GM, "attributes", null, null); w.startRepeatingElement(); for (GmAttribute attribute : attributes) { List<XmlWriter.Attribute> attributes = new ArrayList<XmlWriter.Attribute>(); GoogleBaseAttributeId id = attribute.getAttributeId(); attributes.add(new XmlWriter.Attribute("name", id.getName())); if (id.getType() != null) { attributes.add(new XmlWriter.Attribute("type", id.getType().getName())); } if (attribute.getImportance() != null) { attributes.add(new XmlWriter.Attribute("importance", attribute.getImportance().getXmlValue())); } w.simpleElement(GoogleBaseNamespaces.GM, "attribute", attributes, null); } w.endRepeatingElement(); w.endElement(); }
/** * Creates a {@link GoogleBaseAttribute} of type * {@link GoogleBaseAttributeType#GROUP} and initializes it using the * current state of the object. Sub-attributes of group with type * GROUP or GROUP's derived types are ignored. * * @param name attribute name * @param group attribute value * @return a new {@link com.google.api.gbase.client.GoogleBaseAttribute} */ public static GoogleBaseAttribute createAttribute(String name, Group group) { GoogleBaseAttribute attribute = new GoogleBaseAttribute(name, GoogleBaseAttributeType.GROUP); Multimap<String, GoogleBaseAttribute> subAttributes = group.getAllSubAttributes(); for (GoogleBaseAttribute attr : subAttributes.values()) { GoogleBaseAttributeType type = attr.getType(); if (!GoogleBaseAttributeType.GROUP.equals(type) && !GoogleBaseAttributeType.GROUP.equals(type.getSupertype()) && (attr.hasValue() || attr.hasSubElements())) { attribute.addSubAttribute(attr); } } return attribute; }
@Override public boolean equals(Object o) { if (!(o instanceof GoogleBaseAttributeId)) { return false; } GoogleBaseAttributeId other = (GoogleBaseAttributeId)o; if (!name.equals(other.name)) { return false; } if (type == null) { return other.type == null; } else { return type.equals(other.type); } } }
/** * Given an XML element local name and its attributes, creates * an {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * This method reads the 'type' attribute and set it in the * {@link GoogleBaseAttribute}. * * @param attributeName attribute name, with spaces * @param xmlAttributes XML attributes for the current element * @return the GoogleBaseAttribute for the XML tag */ private GoogleBaseAttribute createExtensionAttribute( String attributeName, Attributes xmlAttributes) { String type = xmlAttributes.getValue("type"); String access = xmlAttributes.getValue("access"); boolean privateAccess = "private".equals(access); return new GoogleBaseAttribute(attributeName, GoogleBaseAttributeType.getInstance(type), privateAccess); }
private boolean hasNameAndType(GoogleBaseAttribute attr, String name, GoogleBaseAttributeType type) { GoogleBaseAttributeType subtype = attr.getAttributeId().getType(); return name.equals(attr.getAttributeId().getName()) && (type == null || subtype != null && type.isSupertypeOf(subtype)); }
/** * Returns the XML attributes that should be set to the * element corresponding to the given * {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * This method is meant to be implemented in subclasses that need * so set attributes. By default, it defines no attributes and * simply returns null. * * @param attribute * @return a collection of XML attributes or null */ private Collection<XmlWriter.Attribute> getXmlAttributes( GoogleBaseAttribute attribute) { GoogleBaseAttributeType type = attribute.getAttributeId().getType(); if (type == null) { return null; } List<XmlWriter.Attribute> attributes = new ArrayList<XmlWriter.Attribute>(); attributes.add(new XmlWriter.Attribute("type", type.toString())); if (attribute.isPrivate()) { attributes.add(new XmlWriter.Attribute("access", "private")); } return attributes; }
@Override public int hashCode() { int retval = 19 + name.hashCode(); if (type != null) { retval = retval * 37 + type.hashCode(); } return retval; }
/** * Get a type instance. * * @param typeName name of a type found in a feed. Current version of * the API will always return one of the types listed in this class * but future versions might support new types, so <code>getInstance</code> * accepts any type name * @return an instance of GoogleBaseAttributeType, or <code>null</code> * if <code>typeName</code> was null */ public static GoogleBaseAttributeType getInstance(String typeName) { if (typeName == null) { return null; } GoogleBaseAttributeType commonType = KNOWN_TYPES.get(typeName); if (commonType != null) { return commonType; } else { return new GoogleBaseAttributeType(null, typeName); } }
/** Generate the XML representation for this tag. */ public void generate(XmlWriter w, ExtensionProfile extProfile) throws IOException { if (attributes.isEmpty()) { // Nothing to write return; } w.startElement(GoogleBaseNamespaces.GM, "attributes", null, null); w.startRepeatingElement(); for (GmAttribute attribute : attributes) { List<XmlWriter.Attribute> attributes = new ArrayList<XmlWriter.Attribute>(); GoogleBaseAttributeId id = attribute.getAttributeId(); attributes.add(new XmlWriter.Attribute("name", id.getName())); if (id.getType() != null) { attributes.add(new XmlWriter.Attribute("type", id.getType().getName())); } if (attribute.getImportance() != null) { attributes.add(new XmlWriter.Attribute("importance", attribute.getImportance().getXmlValue())); } w.simpleElement(GoogleBaseNamespaces.GM, "attribute", attributes, null); } w.endRepeatingElement(); w.endElement(); }
/** * Checks whether the given type is the same as this type or a subtype. * * @param subtype * @return true if <code>subtype</code> is the same type or a subtype * of the current type */ public boolean isSupertypeOf(GoogleBaseAttributeType subtype) { if (this.equals(subtype)) { return true; } GoogleBaseAttributeType otherSupertype = subtype.getSupertype(); if (otherSupertype == null) { return false; } return isSupertypeOf(otherSupertype); } }