⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copyfromhelper.html

📁 解析RSS工具源码
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<a name="84" href="#84">84</a>              }<a name="85" href="#85">85</a>              <strong>else</strong><a name="86" href="#86">86</a>              <strong>if</strong> (value instanceof Collection) {<a name="87" href="#87">87</a>                  value = doCopyCollection((Collection)value,baseInterface);<a name="88" href="#88">88</a>              }<a name="89" href="#89">89</a>              <strong>else</strong><a name="90" href="#90">90</a>              <strong>if</strong> (value instanceof Map) {<a name="91" href="#91">91</a>                  value = doCopyMap((Map)value,baseInterface);<a name="92" href="#92">92</a>              }<a name="93" href="#93">93</a>              <strong>else</strong><a name="94" href="#94">94</a>              <strong>if</strong> (isBasicType(vClass)) {<a name="95" href="#95">95</a>                  <em class="comment">// value = value; // nothing to do here</em><a name="96" href="#96">96</a>                  <strong>if</strong> (value instanceof Date) { <em class="comment">// because Date it is not inmutable</em><a name="97" href="#97">97</a>                      value = ((Date)value).clone();<a name="98" href="#98">98</a>                  }<a name="99" href="#99">99</a>              }<a name="100" href="#100">100</a>             <strong>else</strong> { <em class="comment">// it goes CopyFrom</em><a name="101" href="#101">101</a>                 <strong>if</strong> (value instanceof CopyFrom) {<a name="102" href="#102">102</a>                     <a href="../../../../../com/sun/syndication/feed/CopyFrom.html">CopyFrom</a> source = (CopyFrom) value;<a name="103" href="#103">103</a>                     <a href="../../../../../com/sun/syndication/feed/CopyFrom.html">CopyFrom</a> target = createInstance(source.getInterface());<a name="104" href="#104">104</a>                     target = target == <strong>null</strong> ?  (CopyFrom) value.getClass().newInstance() : target;<a name="105" href="#105">105</a>                     target.copyFrom(source);<a name="106" href="#106">106</a>                     value = target;<a name="107" href="#107">107</a>                 }<a name="108" href="#108">108</a>                 <strong>else</strong> {<a name="109" href="#109">109</a>                     <strong>throw</strong> <strong>new</strong> Exception(<span class="string">"unsupported class for 'copyFrom' "</span>+value.getClass());<a name="110" href="#110">110</a>                 }<a name="111" href="#111">111</a>             }<a name="112" href="#112">112</a>         }<a name="113" href="#113">113</a>         <strong>return</strong> value;<a name="114" href="#114">114</a>     }<a name="115" href="#115">115</a> <a name="116" href="#116">116</a>     <strong>private</strong> Object doCopyArray(Object array,Class baseInterface) throws Exception {<a name="117" href="#117">117</a>         Class elementClass = array.getClass().getComponentType();<a name="118" href="#118">118</a>         <strong>int</strong> length = Array.getLength(array);<a name="119" href="#119">119</a>         Object newArray = Array.newInstance(elementClass,length);<a name="120" href="#120">120</a>         <strong>for</strong> (<strong>int</strong> i=0;i&lt;length;i++) {<a name="121" href="#121">121</a>             Object element = doCopy(Array.get(array,i),baseInterface);<a name="122" href="#122">122</a>             Array.set(newArray,i,element);<a name="123" href="#123">123</a>         }<a name="124" href="#124">124</a>         <strong>return</strong> newArray;<a name="125" href="#125">125</a>     }<a name="126" href="#126">126</a> <a name="127" href="#127">127</a>     <strong>private</strong> Object doCopyCollection(Collection collection,Class baseInterface) throws Exception {<a name="128" href="#128">128</a>         <em class="comment">// expecting SETs or LISTs only, going default implementation of them</em><a name="129" href="#129">129</a>         Collection <strong>new</strong>Coll = (collection instanceof Set) ? (Collection)<strong>new</strong> HashSet() : (Collection)<strong>new</strong> ArrayList();<a name="130" href="#130">130</a>         Iterator i = collection.iterator();<a name="131" href="#131">131</a>         <strong>while</strong> (i.hasNext()) {<a name="132" href="#132">132</a>             Object element = doCopy(i.next(),baseInterface);<a name="133" href="#133">133</a>             newColl.add(element);<a name="134" href="#134">134</a>         }<a name="135" href="#135">135</a>         <strong>return</strong> newColl;<a name="136" href="#136">136</a>     }<a name="137" href="#137">137</a> <a name="138" href="#138">138</a>     <strong>private</strong> Object doCopyMap(Map map,Class baseInterface) throws Exception {<a name="139" href="#139">139</a>         Map <strong>new</strong>Map = <strong>new</strong> HashMap();<a name="140" href="#140">140</a>         Iterator entries = map.entrySet().iterator();<a name="141" href="#141">141</a>         <strong>while</strong> (entries.hasNext()) {<a name="142" href="#142">142</a>             Map.Entry entry = (Map.Entry) entries.next();<a name="143" href="#143">143</a>             Object key = entry.getKey(); <em class="comment">// we are assuming string KEYS</em><a name="144" href="#144">144</a>             Object element = doCopy(entry.getValue(),baseInterface);<a name="145" href="#145">145</a>             newMap.put(key,element);<a name="146" href="#146">146</a>         }<a name="147" href="#147">147</a>         <strong>return</strong> newMap;<a name="148" href="#148">148</a>     }<a name="149" href="#149">149</a> <a name="150" href="#150">150</a>     <strong>private</strong> <strong>static</strong> <strong>final</strong> Set BASIC_TYPES = <strong>new</strong> HashSet();<a name="151" href="#151">151</a> <a name="152" href="#152">152</a>     <strong>static</strong> {<a name="153" href="#153">153</a>         BASIC_TYPES.add(Boolean.<strong>class</strong>);<a name="154" href="#154">154</a>         BASIC_TYPES.add(Byte.<strong>class</strong>);<a name="155" href="#155">155</a>         BASIC_TYPES.add(Character.<strong>class</strong>);<a name="156" href="#156">156</a>         BASIC_TYPES.add(Double.<strong>class</strong>);<a name="157" href="#157">157</a>         BASIC_TYPES.add(Float.<strong>class</strong>);<a name="158" href="#158">158</a>         BASIC_TYPES.add(Integer.<strong>class</strong>);<a name="159" href="#159">159</a>         BASIC_TYPES.add(Long.<strong>class</strong>);<a name="160" href="#160">160</a>         BASIC_TYPES.add(Short.<strong>class</strong>);<a name="161" href="#161">161</a>         BASIC_TYPES.add(String.<strong>class</strong>);<a name="162" href="#162">162</a>         BASIC_TYPES.add(Date.<strong>class</strong>);<a name="163" href="#163">163</a>     }<a name="164" href="#164">164</a> <a name="165" href="#165">165</a>     <strong>private</strong> <strong>boolean</strong> isBasicType(Class vClass) {<a name="166" href="#166">166</a>         <strong>return</strong> BASIC_TYPES.contains(vClass);<a name="167" href="#167">167</a>     }<a name="168" href="#168">168</a> <a name="169" href="#169">169</a> }</pre><hr/><div id="footer">This page was automatically generated by <a href="http://maven.apache.org/">Maven</a></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -