@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } if (obj.isUndefined() || obj.isDeprecated()) { return -1; } assert obj.isNull(); return 0; }
@Override public Void visit(BsonArray value, ByteBuf arg) { final int docStart = arg.writerIndex(); arg.writeInt(0); // reserve space for doc size int i = 0; for (BsonValue<?> child : value) { try { arg.writeByte(ParsingTools.getByte(child.getType())); } catch (NettyBsonReaderException ex) { throw new AssertionError(ex); } writeCString(arg, Integer.toString(i)); child.accept(this, arg); i++; } arg.writeByte(0x00); int docEnd = arg.writerIndex(); arg.writerIndex(docStart).writeInt(docEnd - docStart).writerIndex(docEnd); return null; }
@Nonnull public static BsonObjectId getObjectId(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isObjectId()) { throw new TypesMismatchException(fieldId, "objectId", object.getType()); } return object.asObjectId(); }
public static boolean getBooleanOrNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isNumber()) { return object.asNumber().intValue() != 0; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or numeric type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
public static boolean getBooleanOrUndefined(Entry<?> entry, String fieldId, boolean defaultValue) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isUndefined()) { return defaultValue; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or undefined type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
private boolean getSnapshot(BsonValue<?> value) { switch (value.getType()) { case BOOLEAN: return ((BsonBoolean) value).getPrimitiveValue(); case UNDEFINED: case NULL: return false; default: return true; } }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } assert obj.isTimestamp(); return compareTo(obj.asTimestamp()); }
@Nonnull public static BsonDocument getDocument(Entry<?> entry) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isDocument()) { throw new TypesMismatchException(entry.getKey(), BsonType.DOCUMENT, object.getType()); } return object.asDocument(); }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return 0; } assert obj.isDocument(); BsonDocument other = obj.asDocument(); // TODO: Check how MongoDB compares documents! diff = this.size() - other.size(); if (diff != 0) { return diff; } Iterator<Entry<?>> myIt = this.iterator(); Iterator<Entry<?>> otherIt = other.iterator(); while (myIt.hasNext() && otherIt.hasNext()) { diff = myIt.next().getValue().compareTo(otherIt.next().getValue()); if (diff != 0) { return diff; } } assert !myIt.hasNext() : "the other document has more entries than ourself!"; assert !otherIt.hasNext() : "the other document has less entries than ourself!"; return 0; }
public static boolean isPseudoTrue(Entry<?> entry) { if (entry == null) { return false; } BsonValue<?> value = entry.getValue(); switch (value.getType()) { case INT64: return !value.equals(DefaultBsonValues.newLong(0)); case DOUBLE: return !value.equals(DefaultBsonValues.newDouble(0)); case INT32: return !value.equals(DefaultBsonValues.newInt(0)); case BOOLEAN: return value.asBoolean().getPrimitiveValue(); case UNDEFINED: return false; default: return true; } }
@Nonnull public static BsonNumber<?> getNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isNumber()) { String foundType = object.getType().toString().toLowerCase(Locale.ROOT); throw new TypesMismatchException( fieldId, "numeric", object.getType(), fieldId + " field has non-numeric type " + foundType ); } return object.asNumber(); }
@Nonnull public static Instant getInstant(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException( fieldId, "date", value.getType(), "Expected date type for field " + fieldId + ". Found" + value ); } return value.asDateTime().getValue(); }
public static boolean getBoolean(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isBoolean()) { return object.asBoolean().getValue(); } String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "boolean", object.getType(), "Expected boolean type for field " + fieldId + ". Found " + foundType ); }
public static double getDouble(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isDouble()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected double type for field " + fieldId + ". Found" + foundType ); } return object.asDouble().doubleValue(); }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } if (obj.isDbPointer()) { return -1; } assert obj instanceof BsonObjectId; BsonObjectId other = obj.asObjectId(); byte[] otherBytes; if (obj instanceof AbstractBsonObjectId) { otherBytes = ((AbstractBsonObjectId) obj).getBytesUnsafe(); } else { otherBytes = other.toByteArray(); } byte[] thisBytes = getBytesUnsafe(); for (int i = 0; i < 12; i++) { diff = (thisBytes[i] & 0xFF) - (otherBytes[i] & 0xFF); if (diff != 0) { return diff; } } return 0; }
public static int getInteger(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isInt32()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected integer type for field " + fieldId + ". Found" + foundType ); } return object.asInt32().intValue(); }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } // TODO: Check how MongoDB compares js! assert obj.isJavaScript(); BsonJavaScript other = obj.asJavaScript(); return this.getValue().compareTo(other.getValue()); }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return 0; } assert obj instanceof BsonArray; BsonArray other = obj.asArray(); diff = this.size() - other.size(); if (diff != 0) { return diff; } UnmodifiableIterator<BsonValue<?>> myIt = this.iterator(); UnmodifiableIterator<BsonValue<?>> otherIt = other.iterator(); while (myIt.hasNext() && otherIt.hasNext()) { diff = myIt.next().compareTo(otherIt.next()); if (diff != 0) { return diff; } } assert !myIt.hasNext() : "the other array has more entries than ourself!"; assert !otherIt.hasNext() : "the other array has less entries than ourself!"; return 0; }