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

com.torodb.kvdocument.conversion.mongowp.values.LazyBsonKvDocument Maven / Gradle / Ivy

The newest version!
/*
 * ToroDB
 * Copyright © 2014 8Kdata Technology (www.8kdata.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see .
 */

package com.torodb.kvdocument.conversion.mongowp.values;

import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import com.torodb.kvdocument.conversion.mongowp.MongoWpConverter;
import com.torodb.kvdocument.values.KvDocument;
import com.torodb.kvdocument.values.KvValue;
import com.torodb.mongowp.bson.BsonDocument;
import com.torodb.mongowp.bson.BsonDocument.Entry;
import com.torodb.mongowp.bson.BsonValue;

import java.util.NoSuchElementException;

import javax.annotation.Nonnegative;

public class LazyBsonKvDocument extends KvDocument {

  private static final EntryTranslateFunction ENTRY_TRANSLATE_FUNCTION =
      new EntryTranslateFunction();
  private static final long serialVersionUID = 5869442906673694930L;

  private final BsonDocument wrapped;

  public LazyBsonKvDocument(BsonDocument wrapped) {
    this.wrapped = wrapped;
  }

  @Override
  public UnmodifiableIterator> iterator() {
    return Iterators.unmodifiableIterator(
        Iterators.transform(wrapped.iterator(), ENTRY_TRANSLATE_FUNCTION));
  }

  @Override
  public DocEntry getFirstEntry() throws NoSuchElementException {
    return ENTRY_TRANSLATE_FUNCTION.apply(wrapped.getFirstEntry());
  }

  @Override
  public KvValue get(String key) {
    BsonValue bsonValue = wrapped.get(key);
    return MongoWpConverter.translate(bsonValue);
  }

  private static class EntryTranslateFunction
      implements Function, KvDocument.DocEntry> {

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public DocEntry apply(@Nonnegative Entry input) {
      return new MyDocEntry(input);
    }
  }

  private static class MyDocEntry extends DocEntry {

    private final BsonDocument.Entry entry;

    public MyDocEntry(Entry entry) {
      this.entry = entry;
    }

    @Override
    public String getKey() {
      return entry.getKey();
    }

    @SuppressWarnings("unchecked")
    @Override
    public KvValue getValue() {
      return (KvValue) MongoWpConverter.translate(entry.getValue());
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy