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

com.aerospike.mapper.tools.ResultsUnpacker Maven / Gradle / Ivy

package com.aerospike.mapper.tools;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public interface ResultsUnpacker {
	Object unpack(Object object);
	
    class ListUnpacker implements ResultsUnpacker {
    	private ListUnpacker() {
		}
    	@Override
    	public Object unpack(Object object) {
    		if (object == null) {
    			return null;
    		}
    		else {
    			List list = (List)object;
    			return list.isEmpty() ? null : list.get(0);
    		}
    	}
    	public final static ListUnpacker instance = new ListUnpacker();
    }
    
    class IdentityUnpacker implements ResultsUnpacker {
    	private IdentityUnpacker() {
    	}
    	@Override
    	public Object unpack(Object object) {
    		return object;
    	}
    	public final static IdentityUnpacker instance = new IdentityUnpacker();
    }
    
    class ElementUnpacker implements ResultsUnpacker {
    	Function function;
    	public ElementUnpacker(Function itemMapper) {
    		this.function = itemMapper;
    	}
    	@Override
    	public Object unpack(Object object) {
    		return function.apply(object);
    	}
    }

    class ArrayUnpacker implements ResultsUnpacker {
    	Function function;
    	public ArrayUnpacker(Function itemMapper) {
    		this.function = itemMapper;
    	}
    	@Override
    	public Object unpack(Object object) {
    		if (object == null) {
    			return null;
    		}

    		List source = (List)object;
    		List results = new ArrayList<>(source.size());
    		for (Object thisObject : source) {
    			results.add(function.apply(thisObject));
    		}
    		return results;
    	}
    }
}