/*
* Copyright 2010 Proofpoint, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.proofpoint.json;
import com.google.common.base.Preconditions;
import com.google.inject.Binder;
import com.google.inject.Key;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import com.google.inject.internal.MoreTypes.ParameterizedTypeImpl;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
public class JsonCodecBinder
{
private final Binder binder;
public static JsonCodecBinder jsonCodecBinder(Binder binder)
{
return new JsonCodecBinder(binder);
}
private JsonCodecBinder(Binder binder)
{
this.binder = binder;
}
public void bindJsonCodec(Class<?> type)
{
Preconditions.checkNotNull(type, "type is null");
binder.bind(getJsonCodecKey(type)).toProvider(new JsonCodecProvider(type)).in(Scopes.SINGLETON);
}
public void bindJsonCodec(TypeLiteral<?> type)
{
Preconditions.checkNotNull(type, "type is null");
binder.bind(getJsonCodecKey(type.getType())).toProvider(new JsonCodecProvider(type.getType())).in(Scopes.SINGLETON);
}
public void bindListJsonCodec(Class<?> type)
{
Preconditions.checkNotNull(type, "type is null");
ParameterizedTypeImpl listType = new ParameterizedTypeImpl(null, List.class, type);
binder.bind(getJsonCodecKey(listType)).toProvider(new JsonCodecProvider(listType)).in(Scopes.SINGLETON);
}
public void bindListJsonCodec(JsonCodec<?> type)
{
Preconditions.checkNotNull(type, "type is null");
ParameterizedTypeImpl listType = new ParameterizedTypeImpl(null, List.class, type.getType());
binder.bind(getJsonCodecKey(listType)).toProvider(new JsonCodecProvider(listType)).in(Scopes.SINGLETON);
}
public void bindMapJsonCodec(Class<?> keyType, Class<?> valueType)
{
Preconditions.checkNotNull(keyType, "keyType is null");
Preconditions.checkNotNull(valueType, "valueType is null");
ParameterizedTypeImpl mapType = new ParameterizedTypeImpl(null, Map.class, keyType, valueType);
binder.bind(getJsonCodecKey(mapType)).toProvider(new JsonCodecProvider(mapType)).in(Scopes.SINGLETON);
}
public void bindMapJsonCodec(Class<?> keyType, JsonCodec<?> valueType)
{
Preconditions.checkNotNull(keyType, "keyType is null");
Preconditions.checkNotNull(valueType, "valueType is null");
ParameterizedTypeImpl mapType = new ParameterizedTypeImpl(null, Map.class, keyType, valueType.getType());
binder.bind(getJsonCodecKey(mapType)).toProvider(new JsonCodecProvider(mapType)).in(Scopes.SINGLETON);
}
private Key<JsonCodec<?>> getJsonCodecKey(Type type)
{
return (Key<JsonCodec<?>>) Key.get(new ParameterizedTypeImpl(null, JsonCodec.class, type));
}
}