org.ojai.types.ODate Java Examples

The following examples show how to use org.ojai.types.ODate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestJsonDocumentBuilder.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testPutMapUsingBuilder() {
  JsonDocumentBuilder w = (JsonDocumentBuilder)Json.newDocumentBuilder();
  w.addNewMap();
  w.putNull("a1");
  Map<String, Object> m = new HashMap<String, Object>();
  m.put("k1", "abcd");
  m.put("k2", Arrays.asList(1, 2, 3));
  Map<String, Object> m2 = new HashMap<String, Object>();
  m2.put("k3", ODate.parse("2005-10-22"));
  m2.put("k4", Arrays.asList(1.111, 2.222, 3.333));
  m.put("k5", m2);
  w.put("map", m);
  w.endMap();

  Document r = w.getDocument();
  logger.info(w.asUTF8String());
  assertEquals(2, r.getInt("map.k2[1]"));
}
 
Example #2
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonList() throws Exception {
  Object[] objArr = new Object[11];
  objArr[0] = null;
  objArr[1] = true;
  objArr[2] = (byte) 127;
  objArr[3] = (short) 32767;
  objArr[4] = 2147483647;
  objArr[5] = (long) 123456789;
  objArr[6] = (float) 3.015625;
  objArr[7] = 1.7976931348623157e308;
  objArr[8] = ODate.parse("2015-12-31");
  objArr[9] = OTime.parse("11:59:59");
  objArr[10] = ByteBuffer.wrap("ola".getBytes());

  List<Object> listOfObjs = Arrays.asList(objArr);
  Document doc = Json.newDocument();
  doc.set("objarray", listOfObjs);
  List<Object> newList = doc.getList("objarray");
  assertEquals(newList, listOfObjs);
  assertEquals(listOfObjs, newList);
}
 
Example #3
Source File: BeanWithAllTypes.java    From ojai with Apache License 2.0 6 votes vote down vote up
public BeanWithAllTypes init() {
  nullT = null;
  booleanT = true;
  stringT = "sample string";
  byteT = Byte.MAX_VALUE;
  shortT = Short.MAX_VALUE;
  intT = Integer.MAX_VALUE;
  longT = Long.MAX_VALUE;
  floatT = Float.MAX_VALUE;
  doubleT = Double.MAX_VALUE;
  bigDecimalT = new BigDecimal("12345678901234567890.123456789");
  dateT = ODate.parse("2015-12-31");
  timeT = OTime.parse("23:59:59");
  timestampT = OTimestamp.parse("2015-06-29T12:33:22.000Z");
  intervalT = new OInterval(1, 6, 25, 32569, 265);
  byteBufferT = ByteBuffer.wrap(JsonUtils.getBytes("sample binary data"));
  intArrayT = new int[] {0, 1, 1, 2, 3, 5, 8, 13, 21, 34};
  objectListT = ImmutableList.of(1, 23.5, 12.0f, "hello", new ChildObject());
  childObjectT = new ChildObject();
  return this;
}
 
Example #4
Source File: TestDateTime.java    From ojai with Apache License 2.0 6 votes vote down vote up
@Test
public void testDates() throws ParseException {
  ODate midnightFeb29th2012 = new ODate(MIDNIGHT_FEB29TH_2012);
  ODate milliSecondBeforeMidnightFeb29th2012 = new ODate(MILLIS_BEFORE_MIDNIGHT_FEB29TH_2012);
  Document doc = Json.newDocument();
  doc.set("midnightFeb29th2012", midnightFeb29th2012);
  doc.set("milliSecondBeforeMidnightFeb29th2012", milliSecondBeforeMidnightFeb29th2012);

  midnightFeb29th2012 = doc.getDate("midnightFeb29th2012");
  milliSecondBeforeMidnightFeb29th2012 = doc.getDate("milliSecondBeforeMidnightFeb29th2012");
  assertEquals("2012-02-29", midnightFeb29th2012.toString());
  assertEquals("2012-02-28", milliSecondBeforeMidnightFeb29th2012.toString());

  assertEquals(doc.getDate("midnightFeb29th2012"), ODate.parse("2012-02-29"));
  assertEquals(doc.getDate("milliSecondBeforeMidnightFeb29th2012"), ODate.parse("2012-02-28"));
}
 
Example #5
Source File: ArtistService.java    From mapr-music with Apache License 2.0 6 votes vote down vote up
private Artist dtoToArtist(ArtistDto artistDto) {
    Artist artist = new Artist();
    PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
    try {
        propertyUtilsBean.copyProperties(artist, artistDto);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        throw new RuntimeException("Can not create Artist from Data Transfer Object", e);
    }

    if (artistDto.getAlbums() != null) {
        List<Album.ShortInfo> albums = artistDto.getAlbums().stream()
                .map(this::albumDtoToShortInfo)
                .collect(Collectors.toList());
        artist.setAlbums(albums);
    }

    if (artistDto.getBeginDateDay() != null) {
        artist.setBeginDate(new ODate(artistDto.getBeginDateDay()));
    }

    if (artistDto.getEndDateDay() != null) {
        artist.setEndDate(new ODate(artistDto.getEndDateDay()));
    }

    return artist;
}
 
Example #6
Source File: DocumentGenerator.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public void writeObject(Object value) throws IOException {
  if (value instanceof Byte) {
    if (inMap()) {
      b.put(currFieldName, ((Byte) value).byteValue());
    } else {
      b.add(((Byte) value).byteValue());
    }
  } else if (value instanceof OInterval) {
    if (inMap()) {
      b.put(currFieldName, ((OInterval) value));
    } else {
      b.add(((OInterval) value));
    }
  } else if (value instanceof ODate) {
    if (inMap()) {
      b.put(currFieldName, ((ODate) value));
    } else {
      b.add(((ODate) value));
    }
  } else if (value instanceof OTime) {
    if (inMap()) {
      b.put(currFieldName, ((OTime) value));
    } else {
      b.add(((OTime) value));
    }
  } else if (value instanceof OTimestamp) {
    if (inMap()) {
      b.put(currFieldName, ((OTimestamp) value));
    } else {
      b.add(((OTimestamp) value));
    }
  } else {
    throw new IllegalStateException("writeObject() called for type " + value.getClass());
  }
}
 
Example #7
Source File: Artist.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
@JsonSetter("end_date")
public void setEndDate(String dateDayString) {

    if (dateDayString == null) {
        this.endDate = null;
        return;
    }

    this.endDate = ODate.parse(dateDayString);
}
 
Example #8
Source File: TestDate.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateParsed() throws ParseException {
  ODate date = ODate.parse("1970-1-2");
  assertEquals(1970, date.getYear());
  assertEquals(1, date.getMonth());
  assertEquals(2, date.getDayOfMonth());
  assertEquals(1, date.toDaysSinceEpoch());
  assertEquals(ISO_DATE_FORMAT.parse("1970-1-2"), date.toDate());

  date = ODate.parse("1970-1-1");
  assertEquals(1970, date.getYear());
  assertEquals(1, date.getMonth());
  assertEquals(1, date.getDayOfMonth());
  assertEquals(0, date.toDaysSinceEpoch());
  assertEquals(ISO_DATE_FORMAT.parse("1970-1-1"), date.toDate());

  date = ODate.parse("1969-12-31");
  assertEquals(1969, date.getYear());
  assertEquals(12, date.getMonth());
  assertEquals(31, date.getDayOfMonth());
  assertEquals(-1, date.toDaysSinceEpoch());
  assertEquals(ISO_DATE_FORMAT.parse("1969-12-31"), date.toDate());

  date = ODate.parse("2015-12-31");
  assertEquals(2015, date.getYear());
  assertEquals(12, date.getMonth());
  assertEquals(31, date.getDayOfMonth());
  assertEquals(16800, date.toDaysSinceEpoch());
  assertEquals(ISO_DATE_FORMAT.parse("2015-12-31"), date.toDate());
}
 
Example #9
Source File: Artist.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
@JsonSetter("begin_date")
public void setBeginDate(String dateDayString) {

    if (dateDayString == null) {
        this.beginDate = null;
        return;
    }

    this.beginDate = ODate.parse(dateDayString);
}
 
Example #10
Source File: TestDate.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateFromFields() throws ParseException {
  ODate date = new ODate(2015, 12, 31);
  assertEquals(2015, date.getYear());
  assertEquals(12, date.getMonth());
  assertEquals(31, date.getDayOfMonth());
  assertEquals(16800, date.toDaysSinceEpoch());
  assertEquals(ISO_DATE_FORMAT.parse("2015-12-31"), date.toDate());
}
 
Example #11
Source File: Album.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
@JsonSetter("released_date")
public void setReleasedDate(String dateDayString) {

    if (dateDayString == null) {
        this.releasedDate = null;
        return;
    }

    this.releasedDate = ODate.parse(dateDayString);
}
 
Example #12
Source File: AlbumMutationBuilder.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
public AlbumMutationBuilder setDateDay(ODate dateDay, boolean setNullValue) {

        if (dateDay == null && !setNullValue) {
            return this;
        }

        this.mutation.set(RELEASED_DATE_FIELD, dateDay);
        return this;
    }
 
Example #13
Source File: AlbumService.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
private Album dtoToAlbum(AlbumDto albumDto) {

        Album album = new Album();
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        try {
            propertyUtilsBean.copyProperties(album, albumDto);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException("Can not create album Data Transfer Object", e);
        }

        if (albumDto.getTrackList() != null && !albumDto.getTrackList().isEmpty()) {

            List<Track> trackList = albumDto.getTrackList().stream()
                    .map(this::dtoToTrack)
                    .collect(toList());

            album.setTrackList(trackList);
        }

        if (albumDto.getArtistList() != null && !albumDto.getArtistList().isEmpty()) {

            List<Artist.ShortInfo> artistShortInfoList = albumDto.getArtistList().stream()
                    .map(this::artistDtoToShortInfo)
                    .collect(toList());

            album.setArtists(artistShortInfoList);
        }

        if (albumDto.getReleasedDateDay() != null) {
            album.setReleasedDate(new ODate(albumDto.getReleasedDateDay()));
        }

        return album;
    }
 
Example #14
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateWithIntegerMaxMin() {
  Document doc = Json.newDocument();
  ODate d1 = new ODate(Integer.MAX_VALUE);
  ODate d2 = new ODate(Integer.MIN_VALUE);
  doc.set("maxdate", d1);
  doc.set("boolean", false);
  doc.set("mindate", d2);

  logger.info("{}", d1);
  logger.info("{}", doc.getDate("maxdate"));

  assertEquals(true, doc.getValue("maxdate").equals(d1));
  assertEquals(true, doc.getValue("mindate").equals(d2));
}
 
Example #15
Source File: JsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public ODate getDate(FieldPath field) {
  JsonValue v = getKeyValueAt(field.iterator());
  if (v != null) {
    return v.getDate();
  }
  return null;
}
 
Example #16
Source File: TestJsonUtil.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testValuesAsJsonString() {
  Document r = Json.newDocument();
  r.set("a", (long)1234);
  r.set("b", ODate.parse("2011-09-15"));

  assertEquals("{\"$numberLong\":1234}", Values.asJsonString(r.getValue("a")));
}
 
Example #17
Source File: TestJsonDocumentEquals.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateTimeEquals() {
  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

  // original time
  cal.set(2015, 12, 31, 11, 59, 59);
  long ts1 = cal.getTime().getTime();

  // change date, keep the time same
  cal.set(2015, 12, 30, 11, 59, 59);
  long ts2 = cal.getTime().getTime();

  // change time, keep the date same
  cal.set(2015, 12, 31, 11, 59, 58);
  long ts3 = cal.getTime().getTime();


  Document r = Json.newDocument()
      .set("date1", new ODate(ts1))
      .set("date2", new ODate(ts2))
      .set("date3", new ODate(ts3))
      .set("time1", new OTime(ts1))
      .set("time2", new OTime(ts2))
      .set("time3", new OTime(ts3));

  assertEquals(r.getValue("date1"), new ODate(ts1));
  assertNotEquals(r.getValue("date1"), new ODate(ts2));
  assertEquals(r.getValue("date1"), new ODate(ts3));
  assertEquals(r.getValue("date1"), r.getValue("date3"));
  assertNotEquals(r.getValue("date1"), r.getValue("date2"));

  assertEquals(r.getValue("time1"), new OTime(ts1));
  assertEquals(r.getValue("time1"), new OTime(ts2));
  assertNotEquals(r.getValue("time1"), new OTime(ts3));
  assertEquals(r.getValue("time1"), r.getValue("time2"));
  assertNotEquals(r.getValue("time1"), r.getValue("time3"));
}
 
Example #18
Source File: JsonValue.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Override
public ODate getDate() {
  checkType(Type.DATE);
  if (objValue == null) {
    objValue = ODate.fromDaysSinceEpoch((int) jsonValue);
  }
  return (ODate) objValue;
}
 
Example #19
Source File: TypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
private ODate convertValueToDate(EventType oldEvt, EventType evt) {
  switch (oldEvt) {
  case BYTE: case SHORT: case INT: case LONG:
    return new ODate(getCurrentLongValue());
  case STRING:
    return ODate.parse((String)getCurrentObj());
  default:
    throw unsupportedConversion(oldEvt, evt);
  }
}
 
Example #20
Source File: TestTypeMappedJsonDocumentReader.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeMappingsMultiLevelWithTags() throws IOException {
  Map<FieldPath, Value.Type> fieldPathTypeMap = Maps.newHashMap();
  fieldPathTypeMap.put(FieldPath.parseFrom("map.boolean"), Value.Type.BOOLEAN);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.string"), Value.Type.STRING);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.byte"), Value.Type.BYTE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.short"), Value.Type.SHORT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.int"), Value.Type.INT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.long"), Value.Type.LONG);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.float"), Value.Type.FLOAT);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.double"), Value.Type.DOUBLE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.decimal"), Value.Type.DECIMAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.date"), Value.Type.DATE);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.time"), Value.Type.TIME);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.timestamp"), Value.Type.TIMESTAMP);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.interval"), Value.Type.INTERVAL);
  fieldPathTypeMap.put(FieldPath.parseFrom("map.binary"), Value.Type.BINARY);

  try (InputStream in = getJsonStream("org/ojai/test/data/test4.json");
       DocumentStream jsonRecordStream = Json.newDocumentStream(in, fieldPathTypeMap)) {
    Iterator<Document> docIterator = jsonRecordStream.iterator();
    assertTrue(docIterator.hasNext());
    Document doc = docIterator.next();
    assertTrue(doc.getBoolean("map.boolean"));
    assertEquals("eureka", doc.getString("map.string"));
    assertEquals((byte) 127, doc.getByte("map.byte"));
    assertEquals((short) 32767, doc.getShort("map.short"));
    assertEquals(2147483647, doc.getInt("map.int"));
    assertEquals(9223372036854775807L, doc.getLong("map.long"));
    assertEquals((float) 3.4028235, doc.getFloat("map.float"), 0);
    assertEquals(1.7976931348623157e308, doc.getDouble("map.double"), 0);
    assertEquals(ODate.parse("2012-10-20"), doc.getDate("map.date"));
    assertEquals(OTime.parse("07:42:46.123"), doc.getTime("map.time"));
    assertEquals(OTimestamp.parse("2012-10-20T07:42:46.123-07:00"), doc.getTimestamp("map.timestamp"));
    assertEquals(new OInterval(172800000), doc.getInterval("map.interval"));
    assertEquals(Values.parseBinary("YWJjZA=="), doc.getBinary("map.binary"));
  }
}
 
Example #21
Source File: TestJsonDocument.java    From ojai with Apache License 2.0 5 votes vote down vote up
@Test
public void testDate() {
  Document doc = Json.newDocument();
  doc.set("d1", ODate.parse("2005-06-22"));
  ODate d = new ODate(new java.util.Date());
  doc.set("d2", d);
  logger.info("{}", doc.getDate("d1"));
  logger.info("{}", doc.getDate("d2"));

  assertEquals(true, doc.getDate("d1").toString().equals("2005-06-22"));
  assertEquals(true, doc.getDate("d2").toString().equals(d.toString()));
}
 
Example #22
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public ODate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException,
    JsonProcessingException {
  return (ODate) p.getEmbeddedObject();
}
 
Example #23
Source File: DocumentReaderWithProjection.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public ODate getDate() {
  return reader.getDate();
}
 
Example #24
Source File: BeanWithAllTypes.java    From ojai with Apache License 2.0 4 votes vote down vote up
public ODate getDateT() {
  return dateT;
}
 
Example #25
Source File: JacksonHelper.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(ODate value, JsonGenerator jgen, SerializerProvider provider)
    throws IOException, JsonProcessingException {
  jgen.writeObject(value);
}
 
Example #26
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public ODate getDate(FieldPath fieldPath) {
  return wrapped.getDate(fieldPath);
}
 
Example #27
Source File: AlbumMutationBuilder.java    From mapr-music with Apache License 2.0 4 votes vote down vote up
public AlbumMutationBuilder setDateDay(ODate releasedDate) {
    return setDateDay(releasedDate, SET_NULL_VALUE_DEFAULT);
}
 
Example #28
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public ODate getDate(String fieldPath) {
  return wrapped.getDate(fieldPath);
}
 
Example #29
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(String fieldPath, ODate value) {
  throw readOnly();
}
 
Example #30
Source File: ReadOnlyDocument.java    From ojai with Apache License 2.0 4 votes vote down vote up
@Override
public Document set(FieldPath fieldPath, ODate value) {
  throw readOnly();
}