Skip to content

Commit f0ba493

Browse files
committed
Make ListAdapter a generic collection adapter
1 parent e3de6b5 commit f0ba493

File tree

7 files changed

+59
-48
lines changed

7 files changed

+59
-48
lines changed

CHANGELOG.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,20 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7-
## Versions
8-
9-
### Unreleased
7+
## Unreleased
108
There are currently no unreleased changes.
119

12-
### [0.1.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0) (2017-06-29)
10+
## [0.2.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.2.0) (2017-07-06)
11+
12+
### Changed
13+
14+
- Rename `ListMap` to `CollectionAdapter` since it supports any collection.
15+
16+
### Added
17+
18+
- Adapter for `Set` in the list of builtin adapters registered by
19+
`SerializerBuilder::registerBuiltinAdapters()`.
20+
21+
## [0.1.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0) (2017-06-29)
1322

1423
- First beta release

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ to the `dependencies` section of its `pom.xml` file:
5656
<dependency>
5757
<groupId>com.marcospassos</groupId>
5858
<artifactId>phpserializer</artifactId>
59-
<version>0.1.0</version>
59+
<version>0.2.0</version>
6060
</dependency>
6161
</dependencies>
6262
```
@@ -227,8 +227,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
227227
DEALINGS IN THE SOFTWARE.
228228
```
229229

230-
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.1.0-blue.svg
231-
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.1.0%7Cjar
230+
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.2.0-blue.svg
231+
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.2.0%7Cjar
232232
[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer
233233
[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg
234234
[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master
@@ -243,4 +243,4 @@ DEALINGS IN THE SOFTWARE.
243243
[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues
244244
[repository]: https://github.com/marcospassos/java-php-serializer
245245
[releases-page]: https://github.com/marcospassos/java-php-serializer/releases
246-
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0
246+
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.2.0

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.marcospassos</groupId>
88
<artifactId>phpserializer</artifactId>
9-
<version>0.1.0-SNAPSHOT</version>
9+
<version>0.2.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Java PHP Serializer</name>

src/main/java/com/marcospassos/phpserializer/SerializerBuilder.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.marcospassos.phpserializer.adapter.ArrayAdapter;
44
import com.marcospassos.phpserializer.adapter.BooleanAdapter;
55
import com.marcospassos.phpserializer.adapter.IntegerAdapter;
6-
import com.marcospassos.phpserializer.adapter.ListAdapter;
6+
import com.marcospassos.phpserializer.adapter.CollectionAdapter;
77
import com.marcospassos.phpserializer.adapter.MapAdapter;
88
import com.marcospassos.phpserializer.adapter.ObjectAdapter;
99
import com.marcospassos.phpserializer.adapter.ReferableObjectAdapter;
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Set;
1819

1920
/**
2021
* Provides a friendly API for creating instances of {@link Serializer}.
@@ -165,7 +166,8 @@ public SerializerBuilder registerBuiltinAdapters()
165166
registerAdapter(char[].class, arrayAdapter);
166167
registerAdapter(short[].class, arrayAdapter);
167168
registerAdapter(Map.class, new MapAdapter());
168-
registerAdapter(List.class, new ListAdapter());
169+
registerAdapter(List.class, new CollectionAdapter());
170+
registerAdapter(Set.class, new CollectionAdapter());
169171
registerAdapter(Boolean.class, new BooleanAdapter());
170172
registerAdapter(Integer.class, new IntegerAdapter());
171173
registerAdapter(String.class, new StringAdapter());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.marcospassos.phpserializer.adapter;
2+
3+
import com.marcospassos.phpserializer.Context;
4+
import com.marcospassos.phpserializer.TypeAdapter;
5+
import com.marcospassos.phpserializer.Writer;
6+
import java.util.Collection;
7+
8+
/**
9+
* Adapter for handling collections.
10+
*
11+
* @param <T> The type of values in the collection.
12+
*
13+
* @author Marcos Passos
14+
* @since 1.0
15+
*/
16+
public class CollectionAdapter<T> implements TypeAdapter<Collection<T>>
17+
{
18+
@Override
19+
public void write(Collection<T> collection, Writer writer, Context context)
20+
{
21+
int size = collection.size();
22+
23+
writer.writeArrayStart(size);
24+
25+
int index = 0;
26+
for (Object value: collection) {
27+
writer.writeKey(index++);
28+
context.write(value, writer);
29+
}
30+
31+
writer.writeArrayEnd();
32+
}
33+
}

src/main/java/com/marcospassos/phpserializer/adapter/ListAdapter.java

-33
This file was deleted.

src/test/java/com/marcospassos/phpserializer/adapter/ListAdapterTest.java src/test/java/com/marcospassos/phpserializer/adapter/CollectionAdapterTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
import com.marcospassos.phpserializer.Context;
77
import com.marcospassos.phpserializer.Writer;
88
import java.util.Arrays;
9-
import java.util.List;
9+
import java.util.Collection;
1010
import org.junit.Test;
1111
import org.mockito.InOrder;
1212

1313
/**
1414
* @author Marcos Passos
1515
* @since 1.0
1616
*/
17-
public class ListAdapterTest
17+
public class CollectionAdapterTest
1818
{
1919
@Test
2020
public void write() throws Exception
2121
{
22-
ListAdapter<String> adapter = new ListAdapter<>();
22+
CollectionAdapter<String> adapter = new CollectionAdapter<>();
2323
Writer writer = mock(Writer.class);
2424
Context context = mock(Context.class);
25-
List<String> array = Arrays.asList("a", "b", "c");
25+
Collection<String> array = Arrays.asList("a", "b", "c");
2626

2727
adapter.write(array, writer, context);
2828

0 commit comments

Comments
 (0)