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

org.ldaptive.beans.reflect.ArrayReflectionTranscoder Maven / Gradle / Ivy

Go to download

Mapping, persistence, and code generation API for reading and writing POJOs to an LDAP directory

The newest version!
/* See LICENSE for licensing and NOTICE for copyright. */
package org.ldaptive.beans.reflect;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * Reflection transcoder which expects to operate on collections containing an array of values.
 *
 * @author  Middleware Services
 */
public class ArrayReflectionTranscoder implements ReflectionTranscoder
{

  /** Underlying value transcoder. */
  private final SingleValueReflectionTranscoder valueTranscoder;

  /** Type of array element for this transcoder. */
  private final Class type;


  /**
   * Creates a new array reflection transcoder.
   *
   * @param  transcoder  to operate on individual array elements
   */
  public ArrayReflectionTranscoder(final SingleValueReflectionTranscoder transcoder)
  {
    valueTranscoder = transcoder;
    type = Array.newInstance(valueTranscoder.getType(), 0).getClass();
  }


  @Override
  public Object decodeStringValues(final Collection values)
  {
    final Object decoded = Array.newInstance(valueTranscoder.getType(), values.size());
    final Iterator iter = values.iterator();
    for (int i = 0; i < values.size(); i++) {
      final List l = new ArrayList<>(1);
      l.add(iter.next());
      Array.set(decoded, i, valueTranscoder.decodeStringValues(l));
    }
    return decoded;
  }


  @Override
  public Object decodeBinaryValues(final Collection values)
  {
    final Object decoded = Array.newInstance(valueTranscoder.getType(), values.size());
    final Iterator iter = values.iterator();
    for (int i = 0; i < values.size(); i++) {
      final List l = new ArrayList<>(1);
      l.add(iter.next());
      Array.set(decoded, i, valueTranscoder.decodeBinaryValues(l));
    }
    return decoded;
  }


  @Override
  public Collection encodeStringValues(final Object values)
  {
    final List encoded = new ArrayList<>();
    if (values instanceof Object[]) {
      for (Object o : (Object[]) values) {
        encoded.addAll(valueTranscoder.encodeStringValues(o));
      }
    } else {
      if (values instanceof boolean[]) {
        for (boolean o : (boolean[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else if (values instanceof double[]) {
        for (double o : (double[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else if (values instanceof float[]) {
        for (float o : (float[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else if (values instanceof int[]) {
        for (int o : (int[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else if (values instanceof long[]) {
        for (long o : (long[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else if (values instanceof short[]) {
        for (short o : (short[]) values) {
          encoded.addAll(valueTranscoder.encodeStringValues(o));
        }
      } else {
        throw new IllegalArgumentException("Unsupported array type: " + values);
      }
    }
    return encoded;
  }


  @Override
  public Collection encodeBinaryValues(final Object values)
  {
    final List encoded = new ArrayList<>();
    if (values instanceof Object[]) {
      for (Object o : (Object[]) values) {
        encoded.addAll(valueTranscoder.encodeBinaryValues(o));
      }
    } else {
      if (values instanceof boolean[]) {
        for (boolean o : (boolean[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else if (values instanceof double[]) {
        for (double o : (double[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else if (values instanceof float[]) {
        for (float o : (float[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else if (values instanceof int[]) {
        for (int o : (int[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else if (values instanceof long[]) {
        for (long o : (long[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else if (values instanceof short[]) {
        for (short o : (short[]) values) {
          encoded.addAll(valueTranscoder.encodeBinaryValues(o));
        }
      } else {
        throw new IllegalArgumentException("Unsupported array type: " + values);
      }
    }
    return encoded;
  }


  @Override
  public Class getType()
  {
    return type;
  }


  @Override
  public boolean supports(final Class t)
  {
    return getType().equals(t);
  }


  @Override
  public String toString()
  {
    return "[" + getClass().getName() + "@" + hashCode() + "::" + "valueTranscoder=" + valueTranscoder + "]";
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy