Search in sources :

Example 1 with UTC

use of java.time.ZoneOffset.UTC in project molgenis by molgenis.

the class PostgreSqlUtils method getPostgreSqlValue.

/**
 * Returns the PostgreSQL value for the given entity attribute
 *
 * @param entity entity
 * @param attr   attribute
 * @return PostgreSQL value
 */
static Object getPostgreSqlValue(Entity entity, Attribute attr) {
    String attrName = attr.getName();
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            return entity.getBoolean(attrName);
        case CATEGORICAL:
        case XREF:
            Entity xrefEntity = entity.getEntity(attrName);
            return xrefEntity != null ? getPostgreSqlValue(xrefEntity, xrefEntity.getEntityType().getIdAttribute()) : null;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            Iterable<Entity> entities = entity.getEntities(attrName);
            return stream(entities.spliterator(), false).map(mrefEntity -> getPostgreSqlValue(mrefEntity, mrefEntity.getEntityType().getIdAttribute())).collect(toList());
        case DATE:
            return entity.getLocalDate(attrName);
        case DATE_TIME:
            // As a workaround for #5674, we don't store milliseconds
            Instant instant = entity.getInstant(attrName);
            return instant != null ? instant.truncatedTo(ChronoUnit.SECONDS).atOffset(UTC) : null;
        case DECIMAL:
            return entity.getDouble(attrName);
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            return entity.getString(attrName);
        case FILE:
            FileMeta fileEntity = entity.getEntity(attrName, FileMeta.class);
            return fileEntity != null ? getPostgreSqlValue(fileEntity, fileEntity.getEntityType().getIdAttribute()) : null;
        case INT:
            return entity.getInt(attrName);
        case LONG:
            return entity.getLong(attrName);
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) Instant(java.time.Instant) Attribute(org.molgenis.data.meta.model.Attribute) String.format(java.lang.String.format) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FileMeta(org.molgenis.data.file.model.FileMeta) Collectors.toList(java.util.stream.Collectors.toList) ChronoUnit(java.time.temporal.ChronoUnit) StreamSupport.stream(java.util.stream.StreamSupport.stream) LocalDate(java.time.LocalDate) UTC(java.time.ZoneOffset.UTC) MolgenisDataException(org.molgenis.data.MolgenisDataException) Entity(org.molgenis.data.Entity) Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) Instant(java.time.Instant) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 2 with UTC

use of java.time.ZoneOffset.UTC in project jpx by jenetics.

the class FiltersTest method splitByDay.

public void splitByDay() {
    final Random random = new Random(1);
    final ZonedDateTime time = ZonedDateTime.of(2017, 1, 1, 0, 0, 0, 0, UTC);
    final AtomicInteger count = new AtomicInteger();
    final List<WayPoint> points = Stream.generate(() -> nextWayPoint(random)).limit(100).map(wp -> wp.toBuilder().time(time.plusHours(count.incrementAndGet())).build()).collect(toList());
    for (TrackSegment list : Filters.splitByDay(TrackSegment.of(points))) {
        System.out.println("------------------------");
        for (WayPoint point : list) {
            System.out.println(point.getTime() + ": " + point);
        }
    }
}
Also used : WayPointTest.nextWayPoint(io.jenetics.jpx.WayPointTest.nextWayPoint) ZonedDateTime(java.time.ZonedDateTime) IOException(java.io.IOException) Random(java.util.Random) Test(org.testng.annotations.Test) Collectors.toList(java.util.stream.Collectors.toList) GPXTest.nextGPX(io.jenetics.jpx.GPXTest.nextGPX) List(java.util.List) Stream(java.util.stream.Stream) ByteArrayInputStream(java.io.ByteArrayInputStream) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TrackTest.nextTrack(io.jenetics.jpx.TrackTest.nextTrack) UTC(java.time.ZoneOffset.UTC) Random(java.util.Random) ZonedDateTime(java.time.ZonedDateTime) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WayPointTest.nextWayPoint(io.jenetics.jpx.WayPointTest.nextWayPoint)

Aggregations

UTC (java.time.ZoneOffset.UTC)2 Collectors.toList (java.util.stream.Collectors.toList)2 GPXTest.nextGPX (io.jenetics.jpx.GPXTest.nextGPX)1 TrackTest.nextTrack (io.jenetics.jpx.TrackTest.nextTrack)1 WayPointTest.nextWayPoint (io.jenetics.jpx.WayPointTest.nextWayPoint)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 ZonedDateTime (java.time.ZonedDateTime)1 ChronoUnit (java.time.temporal.ChronoUnit)1 List (java.util.List)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Stream (java.util.stream.Stream)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 Entity (org.molgenis.data.Entity)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 FileMeta (org.molgenis.data.file.model.FileMeta)1