Skip to content

Commit

Permalink
Removed unused code in Beans util
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyknic committed Aug 12, 2015
1 parent 96a74fb commit 723d3fd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
Expand Up @@ -43,12 +43,11 @@
import com.speedment.core.config.model.aspects.Child; import com.speedment.core.config.model.aspects.Child;
import com.speedment.core.config.model.aspects.Enableable; import com.speedment.core.config.model.aspects.Enableable;
import com.speedment.core.config.model.aspects.Node; import com.speedment.core.config.model.aspects.Node;
import com.speedment.util.Beans;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import static java.util.Objects.requireNonNull;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;


/** /**
Expand Down Expand Up @@ -129,40 +128,48 @@ public Builder(String name) {
} }


public Builder<T> addProjectConsumer(BiConsumer<T, Project> consumer) { public Builder<T> addProjectConsumer(BiConsumer<T, Project> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Project.class, consumer)); aquireListAndAdd(Project.class, consumer);
return this;
} }


public Builder<T> addDbmsConsumer(BiConsumer<T, Dbms> consumer) { public Builder<T> addDbmsConsumer(BiConsumer<T, Dbms> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Dbms.class, consumer)); aquireListAndAdd(Dbms.class, consumer);
return this;
} }


public Builder<T> addSchemaConsumer(BiConsumer<T, Schema> consumer) { public Builder<T> addSchemaConsumer(BiConsumer<T, Schema> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Schema.class, consumer)); aquireListAndAdd(Schema.class, consumer);
return this;
} }


public Builder<T> addTableConsumer(BiConsumer<T, Table> consumer) { public Builder<T> addTableConsumer(BiConsumer<T, Table> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Table.class, consumer)); aquireListAndAdd(Table.class, consumer);
return this;
} }


public Builder<T> addColumnConsumer(BiConsumer<T, Column> consumer) { public Builder<T> addColumnConsumer(BiConsumer<T, Column> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Column.class, consumer)); aquireListAndAdd(Column.class, consumer);
return this;
} }


public Builder<T> addIndexConsumer(BiConsumer<T, Index> consumer) { public Builder<T> addIndexConsumer(BiConsumer<T, Index> consumer) {
return Beans.run(this, () -> aquireListAndAdd(Index.class, consumer)); aquireListAndAdd(Index.class, consumer);
return this;
} }


public Builder<T> addForeignKeyConsumer(BiConsumer<T, ForeignKey> consumer) { public Builder<T> addForeignKeyConsumer(BiConsumer<T, ForeignKey> consumer) {
return Beans.run(this, () -> aquireListAndAdd(ForeignKey.class, consumer)); aquireListAndAdd(ForeignKey.class, consumer);
return this;
} }


public Builder<T> addForeignKeyReferencesThisTableConsumer(BiConsumer<T, ForeignKey> consumer) { public Builder<T> addForeignKeyReferencesThisTableConsumer(BiConsumer<T, ForeignKey> consumer) {
return Beans.run(this, () -> this.foreignKeyReferencesThisTableConsumers.add(Objects.requireNonNull(consumer))); foreignKeyReferencesThisTableConsumers.add(requireNonNull(consumer));
return this;
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <C extends Node> void aquireListAndAdd(Class<C> clazz, BiConsumer<T, C> consumer) { protected <C extends Node> void aquireListAndAdd(Class<C> clazz, BiConsumer<T, C> consumer) {
aquireList(clazz).add(Objects.requireNonNull((BiConsumer<T, Node>) consumer)); aquireList(clazz).add(requireNonNull((BiConsumer<T, Node>) consumer));
} }


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Expand Down
39 changes: 7 additions & 32 deletions src/main/java/com/speedment/util/Beans.java
Expand Up @@ -20,50 +20,25 @@
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Optional; import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Stream;


/** /**
* Utility class with methods for managing generated beans.
* *
* @author pemi * @author pemi
*/ */
public class Beans { public final class Beans {


public static <P, T> T with(final T thizz, final P item, final Consumer<P> consumer) { public static String beanPropertyName(Method method) {
consumer.accept(item); final String getterName = method.getName();
return thizz;
}

public static <P, T> T run(final T thizz, final Runnable runnable) {
runnable.run();
return thizz;
}

public static <P, T> T withSeveral(final T thizz, final P[] restOfParameters, final Consumer<P> consumer) {
Stream.of(restOfParameters).forEach(consumer::accept);
return thizz;
}

public static <P1, P2, T> T with(final T thizz, final P1 firstParameter, final P2 secondParameter, final BiConsumer<P1, P2> biConsumer) {
biConsumer.accept(firstParameter, secondParameter);
return thizz;
}

public static String beanPropertyName(String getterName) {
final int startIndex = (getterName.startsWith("is")) ? 2 : 3; final int startIndex = (getterName.startsWith("is")) ? 2 : 3;
return getterName.substring(startIndex, startIndex + 1).toLowerCase() + getterName.substring(startIndex + 1); return getterName.substring(startIndex, startIndex + 1).toLowerCase() + getterName.substring(startIndex + 1);
} }


public static String beanPropertyName(Method m) { public static Optional<String> getterBeanPropertyNameAndValue(Method method, Object invocationTarget) {
return beanPropertyName(m.getName());
}

public static Optional<String> getterBeanPropertyNameAndValue(Method m, Object invocationTarget) {


Object value; Object value;
try { try {
value = m.invoke(invocationTarget); value = method.invoke(invocationTarget);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
return Optional.empty(); return Optional.empty();
} }
Expand Down Expand Up @@ -91,7 +66,7 @@ public static Optional<String> getterBeanPropertyNameAndValue(Method m, Object i
value = value.getClass().getSimpleName() + "." + ((Enum) value).name(); value = value.getClass().getSimpleName() + "." + ((Enum) value).name();
} }


return Optional.of(beanPropertyName(m.getName()) + " = " + quote + String.valueOf(value) + quote + ";"); return Optional.of(beanPropertyName(method) + " = " + quote + String.valueOf(value) + quote + ";");
} }


/** /**
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/speedment/util/Util.java
Expand Up @@ -28,6 +28,8 @@ public final class Util {
* Support method that can be used in constructors to throw an * Support method that can be used in constructors to throw an
* {@code UnsupportedOperationException} if someone is trying to create an * {@code UnsupportedOperationException} if someone is trying to create an
* instance of the class. * instance of the class.
*
* @param caller the class of the instance that called this method
*/ */
public static void instanceNotAllowed(Class<?> caller) throws UnsupportedOperationException { public static void instanceNotAllowed(Class<?> caller) throws UnsupportedOperationException {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
Expand Down

0 comments on commit 723d3fd

Please sign in to comment.