All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nesscomputing.mongo.BSONEncodingTransformers Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
/**
 * Copyright (C) 2012 Ness Computing, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nesscomputing.mongo;

import java.util.Date;
import java.util.UUID;

import org.bson.Transformer;
import org.joda.time.ReadableInstant;

import com.google.common.base.Preconditions;

final class BSONEncodingTransformers
{
    private BSONEncodingTransformers()
    {
    }

    /**
     * Convert any {@link ReadableInstant} into a {@link Date}.
     */
    static class BsonReadableInstantTransformer implements Transformer
    {
        @Override
        public Object transform(Object o)
        {
            if (o == null) {
                return null;
            }
            Preconditions.checkState(o instanceof ReadableInstant, "can not convert %s", o.getClass().getSimpleName());
            final ReadableInstant instant = ReadableInstant.class.cast(o);
            return new Date(instant.getMillis());
        }
    }

    /**
     * Converts an {@link UUID} into a string.
     */
    static class BsonUuidTransformer implements Transformer
    {
        @Override
        public Object transform(final Object o)
        {
            if (o == null) {
                return null;
            }
            Preconditions.checkState(o instanceof UUID, "can not convert %s", o.getClass().getSimpleName());
            final UUID uuid = UUID.class.cast(o);
            return uuid.toString();
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy