com.datastax.driver.core.TypeCodec.accepts()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(88)

本文整理了Java中com.datastax.driver.core.TypeCodec.accepts()方法的一些代码示例,展示了TypeCodec.accepts()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeCodec.accepts()方法的具体详情如下:
包路径:com.datastax.driver.core.TypeCodec
类名称:TypeCodec
方法名:accepts

TypeCodec.accepts介绍

[英]Return true if this codec is capable of serializing the given javaType.

The implementation is invariant with respect to the passed argument (through the usage of TypeToken#equals(Object)and it's strongly recommended not to modify this behavior. This means that a codec will only ever return true for the exact Java type that it has been created for.

If the argument represents a Java primitive type, its wrapper type is considered instead.
[中]如果此编解码器能够序列化给定的javaType,则返回true。
对于传递的参数,实现是不变的(通过使用TypeToken#equals(Object))并且强烈建议不要修改此行为。这意味着编解码器只会为其创建的exactJava类型返回true。
如果参数表示Java基元类型,则考虑其包装器类型。

代码示例

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

/**
 * 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));
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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);
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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();
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

@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);
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
 }
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

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;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

public boolean accepts(Object value) {
 return value instanceof NumberBox && numberCodec.accepts(((NumberBox) value).getNumber());
}

相关文章