// 
// Decompiled by Procyon v0.6.0
// 

package org.bson.codecs.pojo;

import org.bson.codecs.configuration.CodecConfigurationException;

final class PropertyAccessorImpl<T> implements PropertyAccessor<T>
{
    private final PropertyMetadata<T> propertyMetadata;
    
    PropertyAccessorImpl(final PropertyMetadata<T> propertyMetadata) {
        this.propertyMetadata = propertyMetadata;
    }
    
    @Override
    public <S> T get(final S instance) {
        try {
            if (!this.propertyMetadata.isSerializable()) {
                throw this.getError(null);
            }
            if (this.propertyMetadata.getGetter() != null) {
                return (T)this.propertyMetadata.getGetter().invoke(instance, new Object[0]);
            }
            return (T)this.propertyMetadata.getField().get(instance);
        }
        catch (final Exception e) {
            throw this.getError(e);
        }
    }
    
    @Override
    public <S> void set(final S instance, final T value) {
        try {
            if (this.propertyMetadata.isDeserializable()) {
                if (this.propertyMetadata.getSetter() != null) {
                    this.propertyMetadata.getSetter().invoke(instance, value);
                }
                else {
                    this.propertyMetadata.getField().set(instance, value);
                }
            }
        }
        catch (final Exception e) {
            throw this.setError(e);
        }
    }
    
    PropertyMetadata<T> getPropertyMetadata() {
        return this.propertyMetadata;
    }
    
    private CodecConfigurationException getError(final Exception cause) {
        return new CodecConfigurationException(String.format("Unable to get value for property '%s' in %s", this.propertyMetadata.getName(), this.propertyMetadata.getDeclaringClassName()), cause);
    }
    
    private CodecConfigurationException setError(final Exception cause) {
        return new CodecConfigurationException(String.format("Unable to set value for property '%s' in %s", this.propertyMetadata.getName(), this.propertyMetadata.getDeclaringClassName()), cause);
    }
}
