rome/HowRomeWorks/UnderstandingTheRomeCommonClassesAndInterfaces.html

504 lines
21 KiB
HTML
Raw Permalink Normal View History

2014-04-18 19:37:21 +02:00
<!DOCTYPE html>
<!--
2016-04-24 20:54:49 +02:00
| Generated by Apache Maven Doxia at 2016-04-24
2014-04-18 19:37:21 +02:00
| Rendered using Apache Maven Fluido Skin 1.3.0
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
2014-04-18 19:37:21 +02:00
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="author" content="mkurz" />
<meta name="Date-Creation-yyyymmdd" content="20110814" />
2016-04-24 20:54:49 +02:00
<meta name="Date-Revision-yyyymmdd" content="20160424" />
<meta http-equiv="Content-Language" content="en" />
2014-04-18 19:37:21 +02:00
<title>ROME - Understanding the Rome common classes and interfaces</title>
<link rel="stylesheet" href="../css/apache-maven-fluido-1.3.0.min.css" />
<link rel="stylesheet" href="../css/site.css" />
<link rel="stylesheet" href="../css/print.css" media="print" />
<script type="text/javascript" src="../js/apache-maven-fluido-1.3.0.min.js"></script>
</head>
<body class="topBarDisabled">
2016-04-24 20:54:49 +02:00
2016-04-24 20:54:49 +02:00
<a href="http://github.com/rometools/rome">
<img style="position: absolute; top: 0; right: 0; border: 0; z-index: 10000;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"
alt="Fork me on GitHub">
</a>
2014-04-18 19:37:21 +02:00
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
2016-04-24 20:54:49 +02:00
<a href="../index.html" id="bannerLeft">
2014-04-18 19:37:21 +02:00
<img src="../images/romelogo.png" alt="ROME"/>
</a>
2014-04-18 19:37:21 +02:00
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
2014-04-18 19:37:21 +02:00
<div id="breadcrumbs">
<ul class="breadcrumb">
2014-04-18 19:37:21 +02:00
2016-04-24 20:54:49 +02:00
<li id="publishDate" class="pull-right">Last Published: 2016-04-24</li> <li class="divider pull-right">|</li>
<li id="projectVersion" class="pull-right">Version: 1.7.0-SNAPSHOT</li>
2014-04-18 19:37:21 +02:00
</ul>
</div>
2014-04-18 19:37:21 +02:00
<div class="row-fluid">
<div id="leftColumn" class="span3">
<div class="well sidebar-nav">
2014-04-18 19:37:21 +02:00
<ul class="nav nav-list">
<li class="nav-header">Rome</li>
<li>
<a href="../index.html" title="Overview">
<i class="none"></i>
Overview</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/index.html" title="How Rome Works">
<i class="icon-chevron-down"></i>
How Rome Works</a>
<ul class="nav nav-list">
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToReadASyndicationFeed.html" title="Read a syndication feed">
<i class="none"></i>
Read a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToCreateAndWriteASyndicationFeed.html" title="Create and write a syndication feed">
<i class="none"></i>
Create and write a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToConvertASyndicationFeedFromOneTypeToAnother.html" title="Convert a syndication feed">
<i class="none"></i>
Convert a syndication feed</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialUsingRomeToAggregateManySyndicationFeedsIntoASingleOne.html" title="Aggregate many syndication feeds">
<i class="none"></i>
Aggregate many syndication feeds</a>
</li>
2014-04-18 19:37:21 +02:00
<li class="active">
<a href="#"><i class="none"></i>Common classes and interfaces</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../HowRomeWorks/RomeV0.4TutorialDefiningACustomModuleBeanParserAndGenerator.html" title="Defining a Custom Module">
<i class="none"></i>
Defining a Custom Module</a>
</li>
</ul>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/index.html" title="Tutorials And Articles">
<i class="none"></i>
Tutorials And Articles</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../ROMEReleases/index.html" title="Releases">
<i class="icon-chevron-right"></i>
Releases</a>
</li>
2014-04-18 19:37:21 +02:00
<li>
<a href="../ROMEDevelopmentProposals/index.html" title="ROME Development Proposals">
<i class="none"></i>
ROME Development Proposals</a>
</li>
2016-04-24 20:54:49 +02:00
<li>
<a href="../Modules/index.html" title="Modules">
<i class="icon-chevron-right"></i>
Modules</a>
</li>
<li>
<a href="../Fetcher/index.html" title="Fetcher">
<i class="icon-chevron-right"></i>
Fetcher</a>
</li>
<li>
<a href="../Opml/index.html" title="OPML">
<i class="none"></i>
OPML</a>
</li>
<li>
<a href="../Propono/index.html" title="Propono">
<i class="none"></i>
Propono</a>
</li>
<li>
<a href="../Certiorem/index.html" title="Certiorem">
<i class="icon-chevron-right"></i>
Certiorem</a>
</li>
2014-04-18 19:37:21 +02:00
<li class="nav-header">Project Documentation</li>
2016-04-24 20:54:49 +02:00
2014-04-18 19:37:21 +02:00
<li>
<a href="../project-info.html" title="Project Information">
<i class="icon-chevron-right"></i>
Project Information</a>
</li>
2014-04-18 19:37:21 +02:00
</ul>
<hr class="divider" />
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
2014-04-18 19:37:21 +02:00
<img class="builtBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
</a>
2014-04-18 19:37:21 +02:00
</div>
</div>
</div>
<div id="bodyColumn" class="span9" >
<div class="section">
<h2>Understanding the Rome common classes and interfaces<a name="Understanding_the_Rome_common_classes_and_interfaces"></a></h2>
<p>The Rome common package contains a set of Java classes that provide support for all the basic features Java Beans commonly must have: toString, equals, hashcode and cloning. There is also a simple enumeration base class (missing Java 5.0 already).</p>
<p>By implementing or extending the common classes and interfaces Beans don't have to hand code these functions. This greatly simplifies things when Beans have several properties, collection properties and composite properties.</p>
<p>The common classes use Java Bean instrospection on the properties of the classes extending and using them. This is done recursively on all properties.</p>
<p>All Rome Beans (interfaces and default implementations) leverage and use these classes and interfaces defined in the common package.</p>
<p>Ideally all this classes and interface should be part of a general component outside of Rome as they are not syndication specific (something like a commons-bean component if we use Jakarta Commons naming). They cannot be hidden in an implementation package as Rome public API uses them.</p>
<div class="section">
<h3>ToString and ToStringBean<a name="ToString_and_ToStringBean"></a></h3>
<p>Beans implementing the ToString interface must implement the toString(String prefix) method. This method must print the bean properties names and values, one per per line, separating the name and value with an '=' sign and prefixing the name with the given prefix parameter using the same notation used in the JSP expression language used in JSTL and in JSP 2.0. This must be done recursively for all array, collection and ToString properties.</p>
<p>The ToStringBean class provides an implementation of the ToString interface with the defined behavior. The ToStringBean constructor takes the class definition the ToStringBean class should use for properties scanning -using instrospection- for printing, normally it is the class of the Bean using the ToStringBean. Beans leveraging the ToStringBean implementation can do it using two different patterns.</p></div>
<div class="section">
<h3>Extending ToStringBean<a name="Extending_ToStringBean"></a></h3>
<div class="source">
<pre>
public class MyBean extend ToStringBean {
public MyBean() {
super(MyBean.class);
}
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
}
</pre></div>
<div class="section">
<h4>Using a ToStringBean in delegation mode<a name="Using_a_ToStringBean_in_delegation_mode"></a></h4>
<div class="source">
<pre>
public class MyBean implements ToString {
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
public String toString(String prefix) {
ToStringBean tsBean = new ToStringBean(MyBean.class,this);
return tsBean.toString(prefix);
}
public String toString() {
return toString(&quot;myBean&quot;);
}
}
</pre></div></div></div>
<div class="section">
<h3>EqualBean<a name="EqualBean"></a></h3>
<p>The EqualsBean class provides a recursive introspetion-based implementation of the equals() and hashCode() methods working on the Bean properties. The EqualsBean constructor takes the class definition that should be properties scanned (using introspection) by the equals() and thehashCode() methods. The EqualsBean class works on array, collection, bean and basic type properties. Java Beans leveraging the EqualsBean implementation can do it using two different patterns.</p>
<div class="section">
<h4>Extending EqualsBean<a name="Extending_EqualsBean"></a></h4>
<div class="source">
<pre>
public class MyBean extend EqualsBean {
public MyBean() {
super(MyBean.class);
}
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
}
</pre></div></div>
<div class="section">
<h4>Using a EqualsBean in delegation mode<a name="Using_a_EqualsBean_in_delegation_mode"></a></h4>
<div class="source">
<pre>
public class MyBean implements ToString {
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
public boolean equals(Object obj) {
EqualsBean eBean = new EqualsBean(MyBean.class,this);
return eBean.beanEquals(obj);
}
public int hashCode() {
EqualsBean equals = new EqualsBean(MyBean.class,this);
return equals.beanHashCode();
}
}
</pre></div></div></div>
<div class="section">
<h3>CloneableBean<a name="CloneableBean"></a></h3>
<p>The CloneableBean class provides a recursive introspetion-based implementation of the clone() method working on the Bean properties. The CloneableBean class works on array, collection, bean and basic type properties. Java Beans leveraging the CloneableBean implementation can do it using two different patterns.</p>
<div class="section">
<h4>Extending CloneableBean<a name="Extending_CloneableBean"></a></h4>
<div class="source">
<pre>
public class MyBean extend CloneableBean {
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
}
</pre></div></div>
<div class="section">
<h4>Using a CloneableBean in delegation mode<a name="Using_a_CloneableBean_in_delegation_mode"></a></h4>
<div class="source">
<pre>
public class MyBean implements Cloneable {
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
public Object clone() {
CloneableBean cBean = new CloneableBean(this);
return cBean.beanClone();
}
}
</pre></div>
<p>By default, the CloneableBean copies all properties of the given object. It also supports an ignore-properties set, the property names in this set will not be copied to the cloned instance. This is useful for cases where the Bean has convenience properties (properties that are actually references to other properties or properties of properties). For example SyndFeed and SyndEntry beans have convenience properties, publishedDate, author, copyright and categories all of them mapped to properties in the DC Module.</p></div></div>
<div class="section">
<h3>ObjectBean<a name="ObjectBean"></a></h3>
<p>The ObjectBean is a convenience bean providing ToStringBean, EqualsBean and CloneableBean functionality support. Also, ObjectBeans implements the Serializable interface making the beans serializable if all its properties are. Beans extending ObjectBean get toString(), equals(), hashCode() and clone() support as defined above.</p>
<p>And example of using the ObjectBean class is:</p>
<div class="source">
<pre>
public class MyBean extends ObjectBean {
public MyBean() {
super(MyBean.class);
}
public Foo getFoo() { ... }
public void setFoo(Foo foo) { ... }
public String getName() { ... }
public void setName(String name) { ... }
public List getValues() { ... }
public void setValues(List values) { ... }
}
</pre></div></div>
<div class="section">
<h3>Enum<a name="Enum"></a></h3>
<p>The Enum is an enumeration base class [Too bad Java 5.0 is not here yet (Aug/16/2004)] with equals(), hashCode(), toString(), clone() and serialization support. When used as properties of Beans implementing any of the above function it provides a seamless integration.</p>
<p>And example of using the EnumBean class is:</p>
<div class="source">
<pre>
public static class Day extends Enum {
private Day(String name) {
super(name);
}
public static final Day SUNDAY = new Day(&quot;sunday&quot;);
public static final Day MONDAY = new Day(&quot;monday&quot;);
public static final Day TUESDAY = new Day(&quot;tuesday&quot;);
public static final Day WEDNESDAY = new Day(&quot;wednesday&quot;);
public static final Day THURSDAY = new Day(&quot;thursday&quot;);
public static final Day FRIDAY = new Day(&quot;friday&quot;);
public static final Day SATURDAY = new Day(&quot;saturday&quot;);
}
</pre></div></div>
<div class="section">
<h3>CopyFrom<a name="CopyFrom"></a></h3>
<p>The CopyFrom interface defines functionality similar to a deep cloning. The difference with the clone() method (besides the deep cloning requirements) is that the object to be the copy of the original one has to be explicitly created and it is this object the one that triggers the deep copying process. Implemetations of the CopyFrom interface should work propertly on arrays, collections, CopyFrom and basic type properties.</p>
<p>Using CopyFrom objects enables copying data between different implementations of a given interface without these implementation having to know about each other.</p>
<p>CopyFrom is unidirectional. A class implementing the CopyFrom knows how to extract properties from the given class (commonly having an interface in common).</p>
<p>A simple example using the CopyFrom interface is:</p>
<div class="source">
<pre>
public interface Foo extends CopyFrom {
public void setName(String name);
public String getName();
public void setValues(Set values);
public Set getValues();
}
public class FooImplA implements Foo {
private String _name;
private Set _values;
public void setName(String name) {
_name = name;
}
public String getName() {
return _name;
}
public void setValues(Set values) {
_values = values;
}
public Set getValues() {
return _values;
}
public void copyFrom(Object obj) {
Foo other = (Foo) obj;
setName(other.getName());
setValues(new HashSet(other.getValues());
}
public Class getInterface() {
return Foo.class;
}
}
public class FooImplB implements Foo {
private Map _data;
public FooImplB() {
_data = new HashMap();
}
public void setName(String name) {
_data.put(&quot;name&quot;,name);
}
public String getName() {
return (String) _data.get(&quot;name&quot;);
}
public void setValues(Set values) {
_data.put(&quot;values&quot;,values);
}
public Set getValues() {
return (Set) _data.get(&quot;values&quot;);
}
public void copyFrom(Object obj) {
Foo other = (Foo) obj;
setName(other.getName());
setValues(new HashSet(other.getValues());
}
public Class getInterface() {
return Foo.class;
}
}
</pre></div>
<p>A use case for the CopyFrom functionality is a Java Bean implementation of an interface and a persistency implementation (such as Hibernate) of the the same interface that may add extra persistency related properties to the bean (ie, the 'Id' property as the persistency layer primary key).</p>
<p>For bean, array and collection properties the bean being invoked which copyFrom() method is invoked is responsible for those properties.</p>
<p>For properties implementing the CopyFrom interface, the bean must create a property instance implementing the interface returned by the getInterface() method. This allows the bean doing the copyFrom() to handle specialized subclasses of property elements propertly. This is also applicacle to array and collection properties. The 'modules' property of the SyndFeed and SyndEntry beans is a use case of this feature where the copyFrom() invocation must create different beans subclasses for each type of module, the getInteface() helps to find the right implementation.</p></div></div>
</div>
2014-04-18 19:37:21 +02:00
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
2016-04-24 20:54:49 +02:00
<div class="row span12">Copyright &copy; 2016.
2014-04-18 19:37:21 +02:00
All Rights Reserved.
</div>
2014-04-18 19:37:21 +02:00
</div>
</footer>
</body>
2016-04-24 20:54:49 +02:00
</html>