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

org.immutables.criteria.mongo.codecs.OptionalProvider Maven / Gradle / Ivy

/*
 * Copyright 2019 Immutables Authors and Contributors
 *
 * 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 org.immutables.criteria.mongo.codecs;

import com.google.common.reflect.TypeToken;
import org.bson.BsonReader;
import org.bson.BsonType;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

import java.lang.reflect.ParameterizedType;
import java.util.Optional;

/**
 * Tuple codecs for optionals
 */
class OptionalProvider implements TypedProvider {

  @SuppressWarnings({"rawtypes", "unchecked"})
  @Override
  public  Codec get(TypeToken token, TypedRegistry registry) {
    if (token.getRawType().equals(Optional.class) && (token.getType() instanceof ParameterizedType)) {
      TypeToken newType = TypeToken.of(((ParameterizedType) token.getType()).getActualTypeArguments()[0]);
      Codec delegate = registry.get(newType);
      return new OptionalCodec(token.getRawType(), delegate);
    } else if (token.getRawType().equals(com.google.common.base.Optional.class) && (token.getType() instanceof ParameterizedType)) {
      TypeToken newType = TypeToken.of(((ParameterizedType) token.getType()).getActualTypeArguments()[0]);
      Codec delegate = registry.get(newType);
      return new GuavaCodec(token.getRawType(), delegate);
    }
    return null;
  }

  private static abstract class AbstractOptional implements Codec {
    private final Class encoderClass;
    private final Codec delegate;

    private AbstractOptional(Class encoderClass, Codec delegate) {
      this.encoderClass = encoderClass;
      this.delegate = delegate;
    }

    @Override
    public final O decode(BsonReader reader, DecoderContext decoderContext) {
      if (reader.getCurrentBsonType() == BsonType.NULL) {
        reader.readNull();
        return empty();
      }
      return nullable(delegate.decode(reader, decoderContext));
    }

    @Override
    public final void encode(BsonWriter writer, O value, EncoderContext encoderContext) {
      throw new UnsupportedOperationException("Not yet supported");
    }

    @Override
    public Class getEncoderClass() {
      return encoderClass;
    }

    abstract O empty();

    abstract O nullable(T value);
  }

  private static class OptionalCodec extends AbstractOptional> {

    private OptionalCodec(Class> encoderClass, Codec delegate) {
      super(encoderClass, delegate);
    }

    @Override
    Optional empty() {
      return Optional.empty();
    }

    @Override
    Optional nullable(T value) {
      return Optional.ofNullable(value);
    }
  }

  private static class GuavaCodec extends AbstractOptional> {

    private GuavaCodec(Class> encoderClass, Codec delegate) {
      super(encoderClass, delegate);
    }

    @Override
    com.google.common.base.Optional empty() {
      return com.google.common.base.Optional.absent();
    }

    @Override
    com.google.common.base.Optional nullable(T value) {
      return com.google.common.base.Optional.fromNullable(value);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy