@Override public boolean accepts(Object value) { if (value instanceof Map) { // runtime type ok, now check key and value types Map<?, ?> map = (Map<?, ?>) value; if (map.isEmpty()) return true; Map.Entry<?, ?> entry = map.entrySet().iterator().next(); return keyCodec.accepts(entry.getKey()) && valueCodec.accepts(entry.getValue()); } return false; }
/** * Return {@code true} if this codec is capable of serializing the given {@code javaType}. * * <p>This implementation simply calls {@link #accepts(TypeToken)}. * * @param javaType The Java type this codec should serialize from and deserialize to; cannot be * {@code null}. * @return {@code true} if the codec is capable of serializing the given {@code javaType}, and * {@code false} otherwise. * @throws NullPointerException if {@code javaType} is {@code null}. */ public boolean accepts(Class<?> javaType) { checkNotNull(javaType, "Parameter javaType cannot be null"); return accepts(TypeToken.of(javaType)); }
@Override public TypeCodec<?> load(CacheKey cacheKey) { checkNotNull(cacheKey.cqlType, "Parameter cqlType cannot be null"); if (logger.isTraceEnabled()) logger.trace( "Loading codec into cache: [{} <-> {}]", CodecRegistry.toString(cacheKey.cqlType), CodecRegistry.toString(cacheKey.javaType)); for (TypeCodec<?> codec : codecs) { if (codec.accepts(cacheKey.cqlType) && (cacheKey.javaType == null || codec.accepts(cacheKey.javaType))) { logger.trace("Already existing codec found: {}", codec); return codec; } } return createCodec(cacheKey.cqlType, cacheKey.javaType); } }
@Override public boolean accepts(Object value) { if (getJavaType().getRawType().isAssignableFrom(value.getClass())) { // runtime type ok, now check element type Collection<?> coll = (Collection<?>) value; if (coll.isEmpty()) return true; Object elt = coll.iterator().next(); return eltCodec.accepts(elt); } return false; }
@Override public boolean accepts(DataType cqlType) { // a tuple codec should accept tuple values of a different type, // provided that the latter is contained in this codec's type. return super.accepts(cqlType) && definition.contains((TupleType) cqlType); }
private String formatInitCond() { if (stateTypeCodec.accepts(initCond)) { try { return stateTypeCodec.format(initCond); } catch (RuntimeException e) { LOGGER.info("Failed to format INITCOND literal: {}", initCond); } } return initCond.toString(); }
@SuppressWarnings("unchecked") private <T> TypeCodec<T> findCodec(DataType cqlType, T value) { checkNotNull(value, "Parameter value cannot be null"); if (logger.isTraceEnabled()) logger.trace("Looking for codec [{} <-> {}]", toString(cqlType), value.getClass()); // Look at the built-in codecs first for (TypeCodec<?> codec : BUILT_IN_CODECS) { if ((cqlType == null || codec.accepts(cqlType)) && codec.accepts(value)) { logger.trace("Built-in codec found: {}", codec); return (TypeCodec<T>) codec; } } // Look at the user-registered codecs next for (TypeCodec<?> codec : codecs) { if ((cqlType == null || codec.accepts(cqlType)) && codec.accepts(value)) { logger.trace("Already registered codec found: {}", codec); return (TypeCodec<T>) codec; } } return createCodec(cqlType, value); }
private <T> TypeCodec<T> createCodec(DataType cqlType, T value) { TypeCodec<T> codec = maybeCreateCodec(cqlType, value); if (codec == null) throw notFound(cqlType, TypeToken.of(value.getClass())); // double-check that the created codec satisfies the initial request if ((cqlType != null && !codec.accepts(cqlType)) || !codec.accepts(value)) throw notFound(cqlType, TypeToken.of(value.getClass())); logger.trace("Codec created: {}", codec); return codec; }
@SuppressWarnings("unchecked") private <T> TypeCodec<T> findCodec(DataType cqlType, TypeToken<T> javaType) { checkNotNull(cqlType, "Parameter cqlType cannot be null"); if (logger.isTraceEnabled()) logger.trace("Looking for codec [{} <-> {}]", toString(cqlType), toString(javaType)); // Look at the built-in codecs first for (TypeCodec<?> codec : BUILT_IN_CODECS) { if (codec.accepts(cqlType) && (javaType == null || codec.accepts(javaType))) { logger.trace("Built-in codec found: {}", codec); return (TypeCodec<T>) codec; } } // Look at the user-registered codecs next for (TypeCodec<?> codec : codecs) { if (codec.accepts(cqlType) && (javaType == null || codec.accepts(javaType))) { logger.trace("Already registered codec found: {}", codec); return (TypeCodec<T>) codec; } } return createCodec(cqlType, javaType); }
private <T> TypeCodec<T> createCodec(DataType cqlType, TypeToken<T> javaType) { TypeCodec<T> codec = maybeCreateCodec(cqlType, javaType); if (codec == null) throw notFound(cqlType, javaType); // double-check that the created codec satisfies the initial request // this check can fail specially when creating codecs for collections // e.g. if B extends A and there is a codec registered for A and // we request a codec for List<B>, the registry would generate a codec for List<A> if (!codec.accepts(cqlType) || (javaType != null && !codec.accepts(javaType))) throw notFound(cqlType, javaType); logger.trace("Codec created: {}", codec); return codec; }
public TypeCodecAssert<T> doesNotAccept(TypeToken<?> javaType) { assertThat(actual.accepts(javaType)) .as("Codec %s should not accept %s but it does", actual, javaType) .isFalse(); return this; }
public TypeCodecAssert<T> accepts(TypeToken<?> javaType) { assertThat(actual.accepts(javaType)) .as("Codec %s should accept %s but it does not", actual, javaType) .isTrue(); return this; }
public TypeCodecAssert<T> accepts(Object value) { assertThat(actual.accepts(value)) .as("Codec %s should accept %s but it does not", actual, value) .isTrue(); return this; }
public TypeCodecAssert<T> accepts(DataType cqlType) { assertThat(actual.accepts(cqlType)) .as("Codec %s should accept %s but it does not", actual, cqlType) .isTrue(); return this; }
public TypeCodecAssert<T> doesNotAccept(Class<?> javaType) { assertThat(actual.accepts(javaType)) .as("Codec %s should not accept %s but it does", actual, javaType) .isFalse(); return this; }
public TypeCodecAssert<T> doesNotAccept(DataType cqlType) { assertThat(actual.accepts(cqlType)) .as("Codec %s should not accept %s but it does", actual, cqlType) .isFalse(); return this; }
private <T> TypeCodec<T> mockCodec(DataType cqlType, TypeToken<T> javaType) { @SuppressWarnings("unchecked") TypeCodec<T> newCodec = mock(TypeCodec.class); when(newCodec.getCqlType()).thenReturn(cqlType); when(newCodec.getJavaType()).thenReturn(javaType); when(newCodec.accepts(cqlType)).thenReturn(true); when(newCodec.accepts(javaType)).thenReturn(true); when(newCodec.toString()).thenReturn(String.format("MockCodec [%s <-> %s]", cqlType, javaType)); return newCodec; } }
public TypeCodecAssert<T> accepts(Class<?> javaType) { assertThat(actual.accepts(javaType)) .as("Codec %s should accept %s but it does not", actual, javaType) .isTrue(); return this; }
public TypeCodecAssert<T> doesNotAccept(Object value) { assertThat(actual.accepts(value)) .as("Codec %s should not accept %s but it does", actual, value) .isFalse(); return this; }
public boolean accepts(Object value) { return value instanceof NumberBox && numberCodec.accepts(((NumberBox) value).getNumber()); }