-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
137 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
xstream/src/java/com/thoughtworks/xstream/converters/extended/AtomicBooleanConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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ö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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters