181 lines
8 KiB
HTML
181 lines
8 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 2013-09-27 -->
|
||
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||
|
<title>ROME - The CopyFrom interface</title>
|
||
|
<style type="text/css" media="all">
|
||
|
@import url("../css/maven-base.css");
|
||
|
@import url("../css/maven-theme.css");
|
||
|
@import url("../css/site.css");
|
||
|
</style>
|
||
|
<link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
|
||
|
<meta name="author" content="mkurz" />
|
||
|
<meta name="Date-Creation-yyyymmdd" content="20110815" />
|
||
|
<meta name="Date-Revision-yyyymmdd" content="20130927" />
|
||
|
<meta http-equiv="Content-Language" content="en" />
|
||
|
|
||
|
</head>
|
||
|
<body class="composite">
|
||
|
<div id="banner">
|
||
|
<a href="http://github.com/" id="bannerLeft">
|
||
|
<img src="../images/romelogo.png" alt="ROME" />
|
||
|
</a>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="breadcrumbs">
|
||
|
|
||
|
|
||
|
<div class="xright">
|
||
|
|
||
|
<span id="publishDate">Last Published: 2013-09-27</span>
|
||
|
| <span id="projectVersion">Version: 1.1-SNAPSHOT</span>
|
||
|
</div>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="leftColumn">
|
||
|
<div id="navcolumn">
|
||
|
|
||
|
|
||
|
<h5>Rome</h5>
|
||
|
<ul>
|
||
|
<li class="none">
|
||
|
<a href="../index.html" title="Overview">Overview</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../HowRomeWorks/index.html" title="How Rome Works">How Rome Works</a>
|
||
|
</li>
|
||
|
<li class="none">
|
||
|
<a href="../RssAndAtOMUtilitiEsROMEV0.5AndAboveTutorialsAndArticles/index.html" title="Tutorials And Articles">Tutorials And Articles</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../ROMEReleases/index.html" title="Releases">Releases</a>
|
||
|
</li>
|
||
|
<li class="none">
|
||
|
<a href="../ROMEDevelopmentProposals/index.html" title="ROME Development Proposals">ROME Development Proposals</a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<h5>Project Documentation</h5>
|
||
|
<ul>
|
||
|
<li class="collapsed">
|
||
|
<a href="../project-info.html" title="Project Information">Project Information</a>
|
||
|
</li>
|
||
|
<li class="collapsed">
|
||
|
<a href="../project-reports.html" title="Project Reports">Project Reports</a>
|
||
|
</li>
|
||
|
</ul>
|
||
|
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
|
||
|
<img class="poweredBy" alt="Built by Maven" src="../images/logos/maven-feather.png" />
|
||
|
</a>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="bodyColumn">
|
||
|
<div id="contentBox">
|
||
|
<div class="section">
|
||
|
<h2>The CopyFrom interface<a name="The_CopyFrom_interface"></a></h2>
|
||
|
<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("name",name);
|
||
|
}
|
||
|
|
||
|
public String getName() {
|
||
|
return (String) _data.get("name");
|
||
|
}
|
||
|
|
||
|
public void setValues(Set values) {
|
||
|
_data.put("values",values);
|
||
|
}
|
||
|
|
||
|
public Set getValues() {
|
||
|
return (Set) _data.get("values");
|
||
|
}
|
||
|
|
||
|
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>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
<div id="footer">
|
||
|
<div class="xright">
|
||
|
Copyright © 2004-2013
|
||
|
<a href="http://www.rometools.org">ROME Project</a>.
|
||
|
All Rights Reserved.
|
||
|
|
||
|
</div>
|
||
|
<div class="clear">
|
||
|
<hr/>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|