Skip to content

Commit

Permalink
Make ListAdapter a generic collection adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Jul 7, 2017
1 parent e3de6b5 commit f0ba493
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 48 deletions.
17 changes: 13 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## Versions

### Unreleased
## Unreleased
There are currently no unreleased changes.

### [0.1.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0) (2017-06-29)
## [0.2.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.2.0) (2017-07-06)

### Changed

- Rename `ListMap` to `CollectionAdapter` since it supports any collection.

### Added

- Adapter for `Set` in the list of builtin adapters registered by
`SerializerBuilder::registerBuiltinAdapters()`.

## [0.1.0](https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0) (2017-06-29)

- First beta release
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ to the `dependencies` section of its `pom.xml` file:
<dependency>
<groupId>com.marcospassos</groupId>
<artifactId>phpserializer</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
</dependency>
</dependencies>
```
Expand Down Expand Up @@ -227,8 +227,8 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
```

[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.1.0-blue.svg
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.1.0%7Cjar
[maven-central-badge]: https://img.shields.io/badge/maven%20central-v0.2.0-blue.svg
[maven-central-latest]: http://search.maven.org/#artifactdetails%7Ccom.marcospassos%7Cphpserializer%7C0.2.0%7Cjar
[coverall-status]: https://coveralls.io/github/marcospassos/java-php-serializer
[coverall-badge]: https://coveralls.io/repos/github/marcospassos/java-php-serializer/badge.svg
[travis-badge]: https://travis-ci.org/marcospassos/java-php-serializer.svg?branch=master
Expand All @@ -243,4 +243,4 @@ DEALINGS IN THE SOFTWARE.
[issue-tracker]: https://github.com/marcospassos/java-php-serializer/issues
[repository]: https://github.com/marcospassos/java-php-serializer
[releases-page]: https://github.com/marcospassos/java-php-serializer/releases
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.1.0
[latest-release]: https://github.com/marcospassos/java-php-serializer/releases/tag/0.2.0
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.marcospassos</groupId>
<artifactId>phpserializer</artifactId>
<version>0.1.0-SNAPSHOT</version>
<version>0.2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Java PHP Serializer</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.marcospassos.phpserializer.adapter.ArrayAdapter;
import com.marcospassos.phpserializer.adapter.BooleanAdapter;
import com.marcospassos.phpserializer.adapter.IntegerAdapter;
import com.marcospassos.phpserializer.adapter.ListAdapter;
import com.marcospassos.phpserializer.adapter.CollectionAdapter;
import com.marcospassos.phpserializer.adapter.MapAdapter;
import com.marcospassos.phpserializer.adapter.ObjectAdapter;
import com.marcospassos.phpserializer.adapter.ReferableObjectAdapter;
Expand All @@ -15,6 +15,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Provides a friendly API for creating instances of {@link Serializer}.
Expand Down Expand Up @@ -165,7 +166,8 @@ public SerializerBuilder registerBuiltinAdapters()
registerAdapter(char[].class, arrayAdapter);
registerAdapter(short[].class, arrayAdapter);
registerAdapter(Map.class, new MapAdapter());
registerAdapter(List.class, new ListAdapter());
registerAdapter(List.class, new CollectionAdapter());
registerAdapter(Set.class, new CollectionAdapter());
registerAdapter(Boolean.class, new BooleanAdapter());
registerAdapter(Integer.class, new IntegerAdapter());
registerAdapter(String.class, new StringAdapter());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.marcospassos.phpserializer.adapter;

import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.TypeAdapter;
import com.marcospassos.phpserializer.Writer;
import java.util.Collection;

/**
* Adapter for handling collections.
*
* @param <T> The type of values in the collection.
*
* @author Marcos Passos
* @since 1.0
*/
public class CollectionAdapter<T> implements TypeAdapter<Collection<T>>
{
@Override
public void write(Collection<T> collection, Writer writer, Context context)
{
int size = collection.size();

writer.writeArrayStart(size);

int index = 0;
for (Object value: collection) {
writer.writeKey(index++);
context.write(value, writer);
}

writer.writeArrayEnd();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.Writer;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import org.junit.Test;
import org.mockito.InOrder;

/**
* @author Marcos Passos
* @since 1.0
*/
public class ListAdapterTest
public class CollectionAdapterTest
{
@Test
public void write() throws Exception
{
ListAdapter<String> adapter = new ListAdapter<>();
CollectionAdapter<String> adapter = new CollectionAdapter<>();
Writer writer = mock(Writer.class);
Context context = mock(Context.class);
List<String> array = Arrays.asList("a", "b", "c");
Collection<String> array = Arrays.asList("a", "b", "c");

adapter.write(array, writer, context);

Expand Down

0 comments on commit f0ba493

Please sign in to comment.