-
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.
- Loading branch information
Showing
2 changed files
with
56 additions
and
1 deletion.
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
/* | ||
* 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 9 September 2022 by Basil Crow. | ||
*/ | ||
package com.thoughtworks.xstream.converters.extended; | ||
|
||
import com.thoughtworks.xstream.converters.Converter; | ||
import com.thoughtworks.xstream.converters.MarshallingContext; | ||
import com.thoughtworks.xstream.converters.SingleValueConverterWrapper; | ||
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; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** Converts an {@link AtomicBoolean}. */ | ||
public class AtomicBooleanConverter implements Converter { | ||
|
||
@Override | ||
public boolean canConvert(Class<?> type) { | ||
return type != null && AtomicBoolean.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public void marshal( | ||
Object source, HierarchicalStreamWriter writer, MarshallingContext context) { | ||
final AtomicBoolean atomicBoolean = (AtomicBoolean) source; | ||
writer.startNode("value"); | ||
context.convertAnother( | ||
atomicBoolean.get(), new SingleValueConverterWrapper(BooleanConverter.BINARY)); | ||
writer.endNode(); | ||
} | ||
|
||
@Override | ||
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { | ||
reader.moveDown(); | ||
Object item = | ||
context.convertAnother( | ||
null, | ||
Boolean.class, | ||
new SingleValueConverterWrapper(BooleanConverter.BINARY)); | ||
boolean value = item instanceof Boolean ? ((Boolean) item).booleanValue() : false; | ||
reader.moveUp(); | ||
return new AtomicBoolean(value); | ||
} | ||
} |