Skip to content

Commit

Permalink
Add converter for AtomicBoolean. Closes #308 and #309.
Browse files Browse the repository at this point in the history
  • Loading branch information
joehni committed Dec 29, 2022
1 parent 1b2e3a3 commit 37cab19
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
21 changes: 19 additions & 2 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,28 @@ <h2>Stream compatibility</h2>
<li>No support for Hibernate 3 collections.</li>
</ul>

<!--
<h1 id="upcoming-1.4.x">Upcoming 1.4.x maintenance release</h1>

<p>Not yet released.</p>
-->

<h2>Major changes</h2>

<ul>
<li>GHI:#308: Add converter for AtomicBoolean of package java.util.concurrent.atomic.</li>
</ul>

<h2>Stream compatibility</h2>

<ul>
<li>The atomic types with new converters of package java.util.concurrent.atomic, that have been written with
previous versions of XStream, can still be deserialized.</li>
</ul>

<h2>API changes</h2>

<ul>
<li>Added c.t.x.converters.extended.AtomicBooleanConverter.</li>
</ul>

<h1 id="1.4.19">1.4.19</h1>

Expand Down
23 changes: 22 additions & 1 deletion xstream-distribution/src/content/converters.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<!--
Copyright (C) 2005, 2006 Joe Walnes.
Copyright (C) 2006, 2007, 2008, 2009, 2011, 2013, 2014, 2015, 2016, 2017, 2019 XStream committers.
Copyright (C) 2006, 2007, 2008, 2009, 2011, 2013, 2014, 2015, 2016, 2017, 2019, 2022 XStream committers.
All rights reserved.
The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -465,6 +465,27 @@
<td>normal</td>
</tr>

<!-- .................................................................................................. -->
<tr>
<td colspan="5" class="headerRow"><h1 id="java.util.concurrent.atomic">java.util.concurrent.atomic</h1></td>
</tr>
<tr>
<th>Converter</th>
<th>Supported types</th>
<th>Example</th>
<th>Notes</th>
<th>Prio</th>
</tr>
<tr>
<td><a href="javadoc/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.html">AtomicBooleanConverter</a></td>
<td>java.util.concurrent.atomic.AtomicBoolean</td>
<td class="example">
&lt;atomic-boolean&gt;true&lt;/atomic-boolean&gt;
</td>
<td>Available with Java 1.5 or greater.</td>
<td>normal</td>
</tr>

<!-- .................................................................................................. -->
<tr>
<td colspan="5" class="headerRow"><h1 id="java.time">java.time</h1></td>
Expand Down
1 change: 1 addition & 0 deletions xstream-distribution/src/content/team.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ <h2 id="contributors">Contributors</h2>
<li>Falko Modler</li>
<li>Julia Boes of Oracle</li>
<li>Chris Hegarty of Oracle</li>
<li>Basil Crow</li>
</ul>

<p>Please direct all correspondence about XStream to the <a href="mailing-lists.html">mailing list</a>
Expand Down
11 changes: 8 additions & 3 deletions xstream/src/java/com/thoughtworks/xstream/XStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

import com.thoughtworks.xstream.converters.ConversionException;
Expand Down Expand Up @@ -141,6 +142,7 @@
import com.thoughtworks.xstream.converters.enums.EnumConverter;
import com.thoughtworks.xstream.converters.enums.EnumMapConverter;
import com.thoughtworks.xstream.converters.enums.EnumSetConverter;
import com.thoughtworks.xstream.converters.extended.AtomicBooleanConverter;
import com.thoughtworks.xstream.converters.extended.CharsetConverter;
import com.thoughtworks.xstream.converters.extended.ColorConverter;
import com.thoughtworks.xstream.converters.extended.CurrencyConverter;
Expand Down Expand Up @@ -716,9 +718,10 @@ protected void setupSecurity() {
allowTypeHierarchy(Path.class);

final Set<Class<?>> types = new HashSet<>();
types.addAll(Arrays.<Class<?>>asList(BitSet.class, Charset.class, Class.class, Currency.class, Date.class,
DecimalFormatSymbols.class, File.class, Locale.class, Object.class, Pattern.class, StackTraceElement.class,
String.class, StringBuffer.class, StringBuilder.class, URL.class, URI.class, UUID.class));
types.addAll(Arrays.<Class<?>>asList(AtomicBoolean.class, BitSet.class, Charset.class, Class.class,
Currency.class, Date.class, DecimalFormatSymbols.class, File.class, Locale.class, Object.class,
Pattern.class, StackTraceElement.class, String.class, StringBuffer.class, StringBuilder.class, URL.class,
URI.class, UUID.class));
if (JVM.isSQLAvailable()) {
types.add(JVM.loadClassForName("java.sql.Timestamp"));
types.add(JVM.loadClassForName("java.sql.Time"));
Expand Down Expand Up @@ -823,6 +826,7 @@ protected void setupAliases() {
alias("linked-hash-map", LinkedHashMap.class);
alias("linked-hash-set", LinkedHashSet.class);
alias("concurrent-hash-map", ConcurrentHashMap.class);
alias("atomic-boolean", AtomicBoolean.class);

alias("enum-set", EnumSet.class);
alias("enum-map", EnumMap.class);
Expand Down Expand Up @@ -954,6 +958,7 @@ protected void setupConverters() {
registerConverter(new BigIntegerConverter(), PRIORITY_NORMAL);
registerConverter(new BigDecimalConverter(), PRIORITY_NORMAL);
registerConverter(new PathConverter(), PRIORITY_NORMAL);
registerConverter((Converter)new AtomicBooleanConverter(), PRIORITY_NORMAL);

registerConverter(new ArrayConverter(mapper), PRIORITY_NORMAL);
registerConverter(new CharArrayConverter(), PRIORITY_NORMAL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2022 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
* Created on 26. November 2022 by Joerg Schaible
*/
package com.thoughtworks.xstream.converters.extended;

import java.util.concurrent.atomic.AtomicBoolean;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;


/**
* Converts an AtomicBoolean type.
*
* @author Basil Crow
* @author J&ouml;rg Schaible
*/
public class AtomicBooleanConverter extends BooleanConverter implements Converter {

/**
* Constructs an AtomicBooleanConverter. Initializes the converter with <em>true</em> and <em>false</em> as
* string representation.
*/
public AtomicBooleanConverter() {
super();
}

@Override
public boolean canConvert(final Class<?> type) {
return type != null && type == AtomicBoolean.class;
}

@Override
public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {
writer.setValue(toString(source));
}

@Override
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context) {
final String data = reader.getValue(); // needs to be called before hasMoreChildren.
if (!reader.hasMoreChildren()) {
return fromString(data);
} else {
// backwards compatibility ... unmarshal nested element
reader.moveDown();
final AtomicBoolean atomicBoolean = new AtomicBoolean("1".equals(reader.getValue()));
reader.moveUp();
return atomicBoolean;
}
}

@Override
public String toString(final Object obj) {
return super.toString(((AtomicBoolean)obj).get() ? Boolean.TRUE : Boolean.FALSE);
}

@Override
public Object fromString(final String str) {
return new AtomicBoolean(((Boolean)super.fromString(str)).booleanValue());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012, 2015, 2017, 2018, 2021 XStream Committers.
* Copyright (C) 2012, 2015, 2017, 2018, 2021, 2022 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -12,14 +12,15 @@

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import com.thoughtworks.xstream.converters.collections.MapConverter;


public class ConcurrentTypesTest extends AbstractAcceptanceTest {

public void testConcurrentHashMap() {
final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
final ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.put("walnes", "joe");
final String xml = xstream.toXML(map);
final String expected = ""
Expand Down Expand Up @@ -55,4 +56,16 @@ public void testDerivedConcurrentHashMap() {

assertBothWays(map, xml);
}

public void testAtomicBoolean() {
final AtomicBoolean atomicBoolean = new AtomicBoolean();
assertBothWays(atomicBoolean, "<atomic-boolean>" + atomicBoolean + "</atomic-boolean>");
}

public void testAtomicBooleanWithOldFormat() {
assertEquals(new AtomicBoolean(true).toString(), xstream.fromXML("" //
+ "<java.util.concurrent.atomic.AtomicBoolean>\n" //
+ " <value>1</value>\n" //
+ "</java.util.concurrent.atomic.AtomicBoolean>").toString());
}
}

0 comments on commit 37cab19

Please sign in to comment.