http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/metrics/MetricsProvider.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/metrics/MetricsProvider.java b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsProvider.java new file mode 100644 index 0000000..78ad9d8 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsProvider.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zest.api.metrics; + +/** + * Metrics Provider SPI. + * <p> + * The Zest Runtime will automatically ook for a service that implements the MetricsProvider interdace + * and use it for internal Runtime metrics, such as the UnitOfWork measuring the time from creation to close. + * </p> + * <p> + * The Metrics Library is available to add metric functionality to applications in the same way, and + * will use the same MetricsProvider. + * </p> + * <p> + * Note that the usual visibitlity rules applies, so you might have more than one MetricsProvider server, + * perhaps per layer. + * </p> + */ +public interface MetricsProvider +{ + /** + * Creates a new factory instance. + * + * The instanctiation is done by providing a Metric type, which is one of + * <ul> + * <li>{@link MetricsCounter}</li> + * <li>{@link MetricsGauge}</li> + * <li>{@link MetricsHealthCheck}</li> + * <li>{@link MetricsHistogram}</li> + * <li>{@link MetricsMeter}</li> + * <li>{@link MetricsTimer}</li> + * </ul> + * + * @param factoryType The class of the metric type needed. + * @param <T> The metric type requested. + * + * @return A factory instance + * + * @throws MetricsNotSupportedException when the MetricsProvider is not supporting the factory type requested. + */ + <T extends MetricsFactory> T createFactory( Class<T> factoryType ) + throws MetricsNotSupportedException; +}
http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimer.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimer.java b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimer.java new file mode 100644 index 0000000..2065e6a --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimer.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zest.api.metrics; + +/** + * Timer Metrics. + */ +public interface MetricsTimer extends Metric +{ + /** + * Start the Timer Metrics. + */ + Context start(); + + /** + * Timer Metrics Context. + */ + public interface Context + { + /** + * Stop the Timer Metrics. + */ + void stop(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimerFactory.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimerFactory.java b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimerFactory.java new file mode 100644 index 0000000..74a7b7f --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/metrics/MetricsTimerFactory.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2012, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zest.api.metrics; + +import java.util.concurrent.TimeUnit; + +/** + * Create MetricsTimer instances. + */ +public interface MetricsTimerFactory extends MetricsFactory +{ + /** + * Create a MetricsTimer instance. + * If the same arguments are given twice, the same instance must be returned. + * + * @param origin The class that instantiate the metric + * @param name A human readable, short name of the metric. + * @param duration the scale unit for this timer's duration metrics + * @param rate the scale unit for this timer's rate metrics + * + * @return A Metric instance to be used, OR org.qi4j.spi.metrics.DefaultMetric.NULL if not supported. + * + */ + MetricsTimer createTimer( Class<?> origin, String name, TimeUnit duration, TimeUnit rate ); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/metrics/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/metrics/package.html b/core/api/src/main/java/org/apache/zest/api/metrics/package.html new file mode 100644 index 0000000..d0280bd --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/metrics/package.html @@ -0,0 +1,21 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<html> + <body> + <h2>Metrics API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/Initializable.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/Initializable.java b/core/api/src/main/java/org/apache/zest/api/mixin/Initializable.java new file mode 100644 index 0000000..be551c2 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/Initializable.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.mixin; + +/** + * Fragments which want to be initialized can implement + * this callback interface. It will be invoked after + * the fragment has bee instantiated and all injections have been done. + */ +public interface Initializable +{ + /** + * Initialize the fragment + * + * @throws org.apache.zest.api.mixin.InitializationException + * if something went wrong + */ + void initialize() + throws InitializationException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/InitializationException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/InitializationException.java b/core/api/src/main/java/org/apache/zest/api/mixin/InitializationException.java new file mode 100644 index 0000000..c46c3af --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/InitializationException.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2007, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.mixin; + +/** + * Thrown when a Fragment or object could not be instantiated. + */ +public class InitializationException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public InitializationException() + { + } + + public InitializationException( String message ) + { + super( message ); + } + + public InitializationException( String message, Throwable cause ) + { + super( message, cause ); + } + + public InitializationException( Throwable cause ) + { + super( cause ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/InvalidMixinException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/InvalidMixinException.java b/core/api/src/main/java/org/apache/zest/api/mixin/InvalidMixinException.java new file mode 100644 index 0000000..10a3375 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/InvalidMixinException.java @@ -0,0 +1,33 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zest.api.mixin; + +import java.lang.reflect.Method; + +/** + * This exception is thrown if a Mixin is invalid (missing method implementation). + */ +public class InvalidMixinException + extends RuntimeException +{ + public InvalidMixinException( Class mixinClass, Method method ) + { + super( mixinClass.getName() + "does not have a method implementation for " + method ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/MixinDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/MixinDescriptor.java b/core/api/src/main/java/org/apache/zest/api/mixin/MixinDescriptor.java new file mode 100644 index 0000000..9ae3600 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/MixinDescriptor.java @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.mixin; + +/** + * Mixin Descriptor. + */ +public interface MixinDescriptor +{ + Class<?> mixinClass(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/MixinMappingException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/MixinMappingException.java b/core/api/src/main/java/org/apache/zest/api/mixin/MixinMappingException.java new file mode 100644 index 0000000..13692a1 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/MixinMappingException.java @@ -0,0 +1,33 @@ +/* + * Copyright 2008 Niclas Hedhman. All rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.mixin; + +/** + * This Exception is thrown when it is not possible to map the MixinType to a valid + * CompositeType. + */ +public class MixinMappingException + extends RuntimeException +{ + private static final long serialVersionUID = 6843167709252705294L; + + public MixinMappingException( String message ) + { + super( message ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/Mixins.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/Mixins.java b/core/api/src/main/java/org/apache/zest/api/mixin/Mixins.java new file mode 100644 index 0000000..1573589 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/Mixins.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2007, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.mixin; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation is used in composites to declare mixin implementation classes. + * <p> + * Mixins tells the runtime which implementation class of a Mixin should be + * used. The @Mixins annotation can occur at any level in the composite hierarchy + * and the runtime will match each found Mixin implementation against a Mixins annotation. + * All mixin interfaces must have a Mixin implementation in the composite hierarchy or + * a runtime exception will occur. + * </p> + * <p> + * Example; + * </p> + * <pre><code> + * + * @Mixins( MyBeerOrder.class ) + * public interface BeerOrderComposite extends BeerOrder, Composite + * { + * } + * + * public class MyBeerOrder + * implements BeerOrder + * { + * : + * } + * </code></pre> + * <p> + * Many implementations can be listed, + * </p> + * <pre><code> + * @Mixins( { MyBeerOrder.class, DescriptionImpl.class } ) + * public interface BeerOrderComposite extends BeerOrder, Description, Composite + * { + * } + * </code></pre> + * <p> + * If the Mixins is a class that implements InvocationHandler, it will be + * used for all mixins. To avoid that an invocation handler based implementation + * not service all mixin, use the AppliesTo annotation. + * </p> + * + * <p> + * It is valid to have multiple Mixins for a mixin. The first one found + * will be used. The search order is in the order they are written in the Mixins + * annotation left-to-right, and depth-first recursive search of the super-interfaces again + * left-to-right. + * </p> + * + * @see org.apache.zest.api.common.AppliesTo + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.TYPE ) +@Documented +public @interface Mixins +{ + Class<?>[] value(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/NoopMixin.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/NoopMixin.java b/core/api/src/main/java/org/apache/zest/api/mixin/NoopMixin.java new file mode 100644 index 0000000..86a8a26 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/NoopMixin.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.mixin; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +/** + * Generic mixin that is a no-op. Can be useful if the functionality + * of a method is mainly provided by concerns and side-effects. + */ +public final class NoopMixin + implements InvocationHandler +{ + private static final Boolean BOOLEAN_DEFAULT = Boolean.FALSE; + private static final Short SHORT_DEFAULT = 0; + private static final Character CHARACTER_DEFAULT = 0; + private static final Integer INTEGER_DEFAULT = 0; + private static final Long LONG_DEFAULT = 0L; + private static final Float FLOAT_DEFAULT = 0f; + private static final Double DOUBLE_DEFAULT = 0.0; + + @Override + public Object invoke( Object object, Method method, Object[] args ) + throws Throwable + { + Class<?> retType = method.getReturnType(); + if( !retType.isPrimitive() ) + { + return null; + } + if( Void.TYPE == retType ) + { + return null; + } + if( Boolean.TYPE == retType ) + { + return BOOLEAN_DEFAULT; + } + if( Short.TYPE == retType ) + { + return SHORT_DEFAULT; + } + if( Character.TYPE == retType ) + { + return CHARACTER_DEFAULT; + } + if( Integer.TYPE == retType ) + { + return INTEGER_DEFAULT; + } + if( Long.TYPE == retType ) + { + return LONG_DEFAULT; + } + if( Float.TYPE == retType ) + { + return FLOAT_DEFAULT; + } + if( Double.TYPE == retType ) + { + return DOUBLE_DEFAULT; + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/mixin/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/mixin/package.html b/core/api/src/main/java/org/apache/zest/api/mixin/package.html new file mode 100644 index 0000000..a0ebe07 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/mixin/package.html @@ -0,0 +1,21 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<html> + <body> + <h2>Mixin API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/object/NoSuchObjectException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/object/NoSuchObjectException.java b/core/api/src/main/java/org/apache/zest/api/object/NoSuchObjectException.java new file mode 100644 index 0000000..35e2655 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/object/NoSuchObjectException.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2008, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.object; + +import org.apache.zest.api.common.InvalidApplicationException; + +/** + * This exception is thrown if no visible Object of the requested type can be found. + */ +public class NoSuchObjectException + extends InvalidApplicationException +{ + private static final long serialVersionUID = -1121690536365682511L; + + private final String objectType; + private final String moduleName; + + public NoSuchObjectException( String type, String moduleName ) + { + super( "Could not find any visible Object of type [" + type + "] in module [" + + moduleName + "]." ); + this.objectType = type; + this.moduleName = moduleName; + } + + public String objectType() + { + return objectType; + } + + public String moduleName() + { + return moduleName; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/object/ObjectDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/object/ObjectDescriptor.java b/core/api/src/main/java/org/apache/zest/api/object/ObjectDescriptor.java new file mode 100644 index 0000000..00c6363 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/object/ObjectDescriptor.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.object; + +import org.apache.zest.api.composite.ModelDescriptor; + +/** + * Object Descriptor. + */ +public interface ObjectDescriptor + extends ModelDescriptor +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/object/ObjectFactory.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/object/ObjectFactory.java b/core/api/src/main/java/org/apache/zest/api/object/ObjectFactory.java new file mode 100644 index 0000000..23b95da --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/object/ObjectFactory.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * Copyright (c) 2007, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.object; + +import org.apache.zest.api.common.ConstructionException; + +/** + * This factory creates and injects POJO's. + */ +public interface ObjectFactory +{ + /** + * Create new objects of the given type. + * + * @param type an object class which will be instantiated. + * + * @return new objects. + * + * @throws ConstructionException Thrown if instantiation fails. + * @throws NoSuchObjectException Thrown if {@code type} class is not an object. + */ + <T> T newObject( Class<T> type, Object... uses ) + throws NoSuchObjectException, ConstructionException; + + /** + * Inject an existing instance. Only fields and methods will be called. + * + * @param instance + * + * @throws ConstructionException + */ + void injectTo( Object instance, Object... uses ) + throws ConstructionException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/object/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/object/package.html b/core/api/src/main/java/org/apache/zest/api/object/package.html new file mode 100644 index 0000000..1e06504 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/object/package.html @@ -0,0 +1,21 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<html> + <body> + <h2>Object API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/package.html b/core/api/src/main/java/org/apache/zest/api/package.html new file mode 100644 index 0000000..ff7d9af --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/package.html @@ -0,0 +1,21 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<html> + <body> + <h2>Apache Zest⢠API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/DefaultValues.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/DefaultValues.java b/core/api/src/main/java/org/apache/zest/api/property/DefaultValues.java new file mode 100644 index 0000000..db6545e --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/DefaultValues.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2008, Michael Hunger. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +/** + * Default values for various property types + */ +public final class DefaultValues +{ + private static final Map<Type, Object> DEFAULT_VALUES = new HashMap<Type, Object>(); + + static + { + DEFAULT_VALUES.put( Byte.class, 0 ); + DEFAULT_VALUES.put( Short.class, 0 ); + DEFAULT_VALUES.put( Character.class, 0 ); + DEFAULT_VALUES.put( Integer.class, 0 ); + DEFAULT_VALUES.put( Long.class, 0L ); + DEFAULT_VALUES.put( Double.class, 0D ); + DEFAULT_VALUES.put( Float.class, 0F ); + DEFAULT_VALUES.put( Boolean.class, false ); + DEFAULT_VALUES.put( String.class, "" ); + } + + public static Object getDefaultValueOf( Type type ) + { + Object value = DEFAULT_VALUES.get( type ); + if( value != null ) + { + return value; + } + if( type instanceof ParameterizedType ) + { + // List<Foo> -> List + type = ( (ParameterizedType) type ).getRawType(); + } + + if( type instanceof Class ) + { + Class typeAsClass = (Class) type; + if( Set.class.isAssignableFrom( typeAsClass ) ) + { + return new HashSet(); + } + else if( Map.class.isAssignableFrom( typeAsClass ) ) + { + return new LinkedHashMap(); + } + else if( Collection.class.isAssignableFrom( typeAsClass ) ) + { + return new ArrayList(); + } + else if( typeAsClass.isEnum() ) + { + return ( (Class) type ).getEnumConstants()[ 0 ]; + } + } + throw new IllegalArgumentException( "Cannot use @UseDefaults with type " + type.toString() ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/GenericPropertyInfo.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/GenericPropertyInfo.java b/core/api/src/main/java/org/apache/zest/api/property/GenericPropertyInfo.java new file mode 100644 index 0000000..5c8e419 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/GenericPropertyInfo.java @@ -0,0 +1,61 @@ +/* + * Copyright 2007,2008 Niclas Hedhman. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.property; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import static org.apache.zest.api.util.Classes.typeOf; + +/** + * Generic Property info utility class. + */ +public final class GenericPropertyInfo +{ + public static Type propertyTypeOf( AccessibleObject accessor ) + { + return toPropertyType( typeOf( accessor ) ); + } + + public static Type toPropertyType( Type methodReturnType ) + { + if( methodReturnType instanceof ParameterizedType ) + { + ParameterizedType parameterizedType = (ParameterizedType) methodReturnType; + if( Property.class.isAssignableFrom( (Class<?>) parameterizedType.getRawType() ) ) + { + return parameterizedType.getActualTypeArguments()[ 0 ]; + } + } + + if( methodReturnType instanceof Class<?> ) + { + Type[] interfaces = ( (Class<?>) methodReturnType ).getGenericInterfaces(); + for( Type anInterface : interfaces ) + { + Type propertyType = toPropertyType( anInterface ); + if( propertyType != null ) + { + return propertyType; + } + } + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/Immutable.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/Immutable.java b/core/api/src/main/java/org/apache/zest/api/property/Immutable.java new file mode 100644 index 0000000..5611a5a --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/Immutable.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation adds Immutability to Types, Properties and Associations + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.TYPE, ElementType.METHOD } ) +@Documented +public @interface Immutable +{ +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/InvalidPropertyTypeException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/InvalidPropertyTypeException.java b/core/api/src/main/java/org/apache/zest/api/property/InvalidPropertyTypeException.java new file mode 100644 index 0000000..8d5b87d --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/InvalidPropertyTypeException.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.zest.api.property; + +import java.lang.reflect.AccessibleObject; +import org.apache.zest.api.common.ConstructionException; + +/** + * Thrown when attempting to subclass Property. + */ +public class InvalidPropertyTypeException extends ConstructionException +{ + public InvalidPropertyTypeException( AccessibleObject accessor ) + { + super( createMessage(accessor) ); + } + + private static String createMessage( AccessibleObject accessor ) + { + StringBuilder builder = new StringBuilder(); + builder.append( "Not allowed to subclass " + Property.class.getName() + ". Property accessor " + accessor + " is returning a Property subclass." ); + return builder.toString(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/Numbers.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/Numbers.java b/core/api/src/main/java/org/apache/zest/api/property/Numbers.java new file mode 100644 index 0000000..2fd39bf --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/Numbers.java @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2009, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.math.BigDecimal; + +/** + * Convenience class for mathematical operations on numerical properties. + * <pre>import static org.qi4j.api.property.Numbers.*; + * ... + * add( object.numberProperty(), 5 );</pre> + */ +public final class Numbers +{ + // Integer operations + + public static Property<Integer> add( Property<Integer> property, int amount ) + { + property.set( property.get() + amount ); + return property; + } + + public static Property<Integer> mult( Property<Integer> property, int amount ) + { + property.set( property.get() * amount ); + return property; + } + + public static Property<Integer> sub( Property<Integer> property, int amount ) + { + property.set( property.get() - amount ); + return property; + } + + public static Property<Integer> div( Property<Integer> property, int amount ) + { + property.set( property.get() / amount ); + return property; + } + + // Long operations + + public static Property<Long> add( Property<Long> property, long amount ) + { + property.set( property.get() + amount ); + return property; + } + + public static Property<Long> mult( Property<Long> property, long amount ) + { + property.set( property.get() * amount ); + return property; + } + + public static Property<Long> sub( Property<Long> property, long amount ) + { + property.set( property.get() - amount ); + return property; + } + + public static Property<Long> div( Property<Long> property, long amount ) + { + property.set( property.get() / amount ); + return property; + } + + // Double operations + + public static Property<Double> add( Property<Double> property, double amount ) + { + property.set( property.get() + amount ); + return property; + } + + public static Property<Double> mult( Property<Double> property, double amount ) + { + property.set( property.get() * amount ); + return property; + } + + public static Property<Double> sub( Property<Double> property, double amount ) + { + property.set( property.get() - amount ); + return property; + } + + public static Property<Double> div( Property<Double> property, double amount ) + { + property.set( property.get() / amount ); + return property; + } + + // Float operations + + public static Property<Float> add( Property<Float> property, float amount ) + { + property.set( property.get() + amount ); + return property; + } + + public static Property<Float> mult( Property<Float> property, float amount ) + { + property.set( property.get() * amount ); + return property; + } + + public static Property<Float> sub( Property<Float> property, float amount ) + { + property.set( property.get() - amount ); + return property; + } + + public static Property<Float> div( Property<Float> property, float amount ) + { + property.set( property.get() / amount ); + return property; + } + + // BigDecimal operations + + public static Property<BigDecimal> add( Property<BigDecimal> property, BigDecimal amount ) + { + property.set( property.get().add( amount ) ); + return property; + } + + public static Property<BigDecimal> mult( Property<BigDecimal> property, BigDecimal amount ) + { + property.set( property.get().multiply( amount ) ); + return property; + } + + public static Property<BigDecimal> sub( Property<BigDecimal> property, BigDecimal amount ) + { + property.set( property.get().subtract( amount ) ); + return property; + } + + public static Property<BigDecimal> div( Property<BigDecimal> property, BigDecimal amount ) + { + property.set( property.get().divide( amount ) ); + return property; + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/Property.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/Property.java b/core/api/src/main/java/org/apache/zest/api/property/Property.java new file mode 100644 index 0000000..9721e92 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/Property.java @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2007-2011, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.property; + +/** + * Properties are declared in Composite interfaces by using this interface. + * <p> + * It creates a first-class object for the property from which you can get and set the value, and access any + * metadata about it. + * </p> + * <p>The type of the Property can be one of the following:</p> + * <ul> + * <li> A boxed primitive (Long,Integer,Boolean, etc.)</li> + * <li> String</li> + * <li> BigInteger</li> + * <li> BigDecimal</li> + * <li> Date</li> + * <li> DateTime (Joda Time)</li> + * <li> LocalDateTime (Joda Time)</li> + * <li> A serializable</li> + * <li> A ValueComposite</li> + * <li> A List, Set or Collection of any of the above</li> + * </ul> + * + * @param <T> Parameterized type of the Property + */ +public interface Property<T> +{ + /** + * Get the value of the property. + * + * @return the value + */ + T get(); + + /** + * Set the value of the property + * + * @param newValue the new value + * + * @throws IllegalArgumentException if the value has an invalid value + * @throws IllegalStateException if the property is immutable + */ + void set( T newValue ) + throws IllegalArgumentException, IllegalStateException; +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/PropertyDescriptor.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/PropertyDescriptor.java b/core/api/src/main/java/org/apache/zest/api/property/PropertyDescriptor.java new file mode 100644 index 0000000..649b1fc --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/PropertyDescriptor.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Type; +import org.apache.zest.api.common.QualifiedName; +import org.apache.zest.api.structure.MetaInfoHolder; +import org.apache.zest.api.structure.Module; +import org.apache.zest.api.type.ValueType; + +/** + * Property Descriptor. + */ +public interface PropertyDescriptor extends MetaInfoHolder +{ + boolean isImmutable(); + + /** + * Get the qualified name of the property which is equal to: + * <pre><code> + * <interface name>:<method name> + * </code></pre> + * + * @return the qualified name of the property + */ + QualifiedName qualifiedName(); + + /** + * Get the type of the property. If the property is declared + * as Property<X> then X is returned. + * + * @return the property type + */ + Type type(); + + AccessibleObject accessor(); + + Object initialValue( Module module ); + + ValueType valueType(); + + boolean queryable(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/PropertyMixin.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/PropertyMixin.java b/core/api/src/main/java/org/apache/zest/api/property/PropertyMixin.java new file mode 100644 index 0000000..4e08f5c --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/PropertyMixin.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2007, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import org.apache.zest.api.common.AppliesTo; +import org.apache.zest.api.common.AppliesToFilter; +import org.apache.zest.api.injection.scope.State; + +/** + * Generic mixin for properties. + */ +// START SNIPPET: actual +@AppliesTo( { PropertyMixin.PropertyFilter.class } ) +public final class PropertyMixin + implements InvocationHandler +{ + @State + private StateHolder state; + + @Override + public Object invoke( Object proxy, Method method, Object[] args ) + throws Throwable + { + return state.propertyFor( method ); + } + + /** + * Filter Property methods to apply generic Property Mixin. + */ + public static class PropertyFilter + implements AppliesToFilter + { + @Override + public boolean appliesTo( Method method, Class<?> mixin, Class<?> compositeType, Class<?> modifierClass ) + { + return Property.class.isAssignableFrom( method.getReturnType() ); + } + } +} +// END SNIPPET: actual http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/PropertyWrapper.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/PropertyWrapper.java b/core/api/src/main/java/org/apache/zest/api/property/PropertyWrapper.java new file mode 100644 index 0000000..f397908 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/PropertyWrapper.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.zest.api.property; + +/** + * If you want to catch getting and setting properties, then create a GenericConcern + * that wraps the Zest-supplied Property instance with PropertyWrappers. Override + * get() and/or set() to perform your custom code. + */ +public class PropertyWrapper + implements Property<Object> +{ + protected Property<Object> next; + + public PropertyWrapper( Property<Object> next ) + { + this.next = next; + } + + public Property<Object> next() + { + return next; + } + + @Override + public Object get() + { + return next.get(); + } + + @Override + public void set( Object newValue ) + throws IllegalArgumentException, IllegalStateException + { + next.set( newValue ); + } + + @Override + public int hashCode() + { + return next.hashCode(); + } + + @Override + public boolean equals( Object obj ) + { + return next.equals( obj ); + } + + @Override + public String toString() + { + return next.toString(); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/StateHolder.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/StateHolder.java b/core/api/src/main/java/org/apache/zest/api/property/StateHolder.java new file mode 100644 index 0000000..d188d3d --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/StateHolder.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.zest.api.property; + +import java.lang.reflect.AccessibleObject; + +/** + * This represents the state of a composite (properties). + */ +public interface StateHolder +{ + /** + * Get a property for a specific accessor + * + * @param accessor of the property + * + * @return the property + * + * @throws IllegalArgumentException if no property for given accessor exists + */ + <T> Property<T> propertyFor( AccessibleObject accessor ) + throws IllegalArgumentException; + + Iterable<? extends Property<?>> properties(); +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/property/package.html ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/property/package.html b/core/api/src/main/java/org/apache/zest/api/property/package.html new file mode 100644 index 0000000..8fc2a93 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/property/package.html @@ -0,0 +1,21 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> +<html> + <body> + <h2>Property API.</h2> + </body> +</html> http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/MissingIndexingSystemException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/MissingIndexingSystemException.java b/core/api/src/main/java/org/apache/zest/api/query/MissingIndexingSystemException.java new file mode 100644 index 0000000..aca06c4 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/MissingIndexingSystemException.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2008, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.query; + +/** + * This Exception is thrown in <code>QueryBuilderFactory.newQueryBuilder()</code> method if + * no indexing subsystem has been declared in the assembly. + */ +public final class MissingIndexingSystemException + extends QueryException +{ + private static final long serialVersionUID = 5147421865890379209L; + + public MissingIndexingSystemException() + { + super( "No EntityFinder has been declared in the assembly of the application." ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/NotQueryableException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/NotQueryableException.java b/core/api/src/main/java/org/apache/zest/api/query/NotQueryableException.java new file mode 100644 index 0000000..403ba4c --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/NotQueryableException.java @@ -0,0 +1,88 @@ +/* + * Copyright 2009 Alin Dreghiciu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.query; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Member; +import org.apache.zest.api.entity.Queryable; +import org.apache.zest.api.property.GenericPropertyInfo; +import org.apache.zest.api.util.Classes; + +/** + * Thrown in case that a non queryable type or accessor (marked with @Queriable(false)) is used during query building, + * or when non-Property, non-Associations are trying to be queried (possibly can not happen). + */ +public class NotQueryableException + extends QueryException +{ + private static final long serialVersionUID = 1L; + + /** + * Constructor. + * + * @param message exception message + */ + public NotQueryableException( final String message ) + { + super( message ); + } + + /** + * Verify that the provided accessor method has not been marked with a Queryable(false). + * + * @param accessor accessor method + * + * @throws NotQueryableException - If accessor method has been marked as not queryable + */ + public static void throwIfNotQueryable( final AccessibleObject accessor ) + { + Queryable queryable = accessor.getAnnotation( Queryable.class ); + if( queryable != null && !queryable.value() ) + { + throw new NotQueryableException( + String.format( + "%1$s \"%2$s\" (%3$s) is not queryable as has been marked with @Queryable(false)", + Classes.RAW_CLASS.map( GenericPropertyInfo.propertyTypeOf( accessor ) ).getSimpleName(), + ( (Member) accessor ).getName(), + ( (Member) accessor ).getDeclaringClass().getName() + ) + ); + } + } + + /** + * Verify that the provided type has not been marked with a Queryable(false). + * + * @param type a type + * + * @throws NotQueryableException - If type has been marked as not queryable + */ + public static void throwIfNotQueryable( final Class<?> type ) + { + Queryable queryable = type.getAnnotation( Queryable.class ); + if( queryable != null && !queryable.value() ) + { + throw new NotQueryableException( + String.format( + "Type \"%1$s\" is not queryable as has been marked with @Queryable(false)", + type.getName() + ) + ); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/Query.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/Query.java b/core/api/src/main/java/org/apache/zest/api/query/Query.java new file mode 100644 index 0000000..84e2c9c --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/Query.java @@ -0,0 +1,138 @@ +/* + * Copyright 2007 Rickard Ãberg. + * Copyright 2007 Niclas Hedhman. + * Copyright 2008 Alin Dreghiciu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.query; + +import java.io.Serializable; +import org.apache.zest.api.property.Property; +import org.apache.zest.api.query.grammar.OrderBy; + +/** + * This represents a Query in an indexing system. It is created from a + * {@link QueryBuilder}, which decides the "where" clause in the query. + * <p> + * Additional limitations, such as paging, ordering, and variables, can be set on + * a Query before it is executed by calling one of find(), iterator(), + * or count(). + * </p> + * <p> + * DDD tip: typically Queries are created in the Domain Model and passed to the UI, + * which sets the order and paging before executing it. + * </p> + */ +public interface Query<T> + extends Iterable<T>, Serializable +{ + /** + * Set the ordering rules. If many segments are used for ordering + * then they will be applied in order. + * + * @param segments the segments to order by + * + * @return the Query + */ + Query<T> orderBy( OrderBy... segments ); + + /** + * Append an ordering rule to the existing segments. + * + * @param property the property to order by + * @param order the order to apply + * + * @return the Query + */ + Query<T> orderBy( final Property<?> property, final OrderBy.Order order ); + + /** + * Append an ascending ordering rule to the existing segments. + * + * @param property the property to order by + * + * @return the Query + */ + Query<T> orderBy( Property<?> property ); + + /** + * Set the index of the first result. Default is 0 (zero). + * + * @param firstResult which index to use as the first one + * + * @return the Query + */ + Query<T> firstResult( int firstResult ); + + /** + * Set how many results should be returned. Default is that + * there is no limit set. + * + * @param maxResults that shouldbe returned + * + * @return the query + */ + Query<T> maxResults( int maxResults ); + + /** + * Get the first Entity that matches the criteria. This + * executes the Query. + * + * @return the first found Entity or null if none were found + * + * @throws QueryExecutionException if the query fails + */ + T find() + throws QueryExecutionException; + + /** + * Set the value of a named variable. + * + * @param name of the variable + * @param value of the variable + * + * @return the query + */ + Query<T> setVariable( String name, Object value ); + + /** + * Get the value of a named variable. + * + * @param name of the variable + * + * @return value of the variable + */ + <V> V getVariable( String name ); + + /** + * Get the result type of this Query + * + * @return the result type + */ + Class<T> resultType(); + + /** + * Count how many results would be returned by this Query. + * This executes the Query. + * + * @return result count + * + * @throws QueryExecutionException if the query fails + */ + long count() + throws QueryExecutionException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/QueryBuilder.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/QueryBuilder.java b/core/api/src/main/java/org/apache/zest/api/query/QueryBuilder.java new file mode 100644 index 0000000..38b010c --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/QueryBuilder.java @@ -0,0 +1,56 @@ +/* + * Copyright 2007 Rickard Ãberg. + * Copyright 2008 Alin Dreghiciu. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.query; + +import org.apache.zest.api.composite.Composite; +import org.apache.zest.functional.Specification; + +/** + * QueryBuilders are used to create {@link Query} instances. + * Iteratively add where() clauses to the query, and then use + * {@link org.apache.zest.api.unitofwork.UnitOfWork#newQuery(QueryBuilder)} to instantiate the Query. + * QueryBuilders are immutable, so when adding new where-clauses you get new instances. This + * + * DDD tip: Query objects are not executed immediately, so they + * should be constructed in the domain model and handed over to + * the UI, which can then further constrain it before actual + * execution. + */ +public interface QueryBuilder<T> +{ + /** + * Add a where-clause to the Query. Use {@link QueryExpressions} + * to create the expression. + * + * @param specification the where clause + * + * @return a new builder with the added where-clause + */ + QueryBuilder<T> where( Specification<Composite> specification ); + + /** + * Create a new query with the declared where-clauses that will be evaluated against the iterable entries. + * + * @param iterable collection of objects (composites?) + * + * @return a new Query instance + */ + Query<T> newQuery( Iterable<T> iterable ); +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/QueryBuilderFactory.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/QueryBuilderFactory.java b/core/api/src/main/java/org/apache/zest/api/query/QueryBuilderFactory.java new file mode 100644 index 0000000..fe40289 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/QueryBuilderFactory.java @@ -0,0 +1,38 @@ +/* + * Copyright 2007 Niclas Hedhman. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.query; + +/** + * This is used to create QueryBuilders. + * + * @see QueryBuilder + */ +public interface QueryBuilderFactory +{ + /** + * Create a new QueryBuilder. + * + * @param resultType the type of the result that you want + * + * @return a QueryBuilder + * + * @throws MissingIndexingSystemException if there is no EntityFinder service available + */ + <T> QueryBuilder<T> newQueryBuilder( Class<T> resultType ) + throws MissingIndexingSystemException; +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/QueryException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/QueryException.java b/core/api/src/main/java/org/apache/zest/api/query/QueryException.java new file mode 100644 index 0000000..a0bd0f6 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/QueryException.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2008, Niclas Hedhman. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.query; + +/** + * Base class for Query exceptions. + */ +public abstract class QueryException + extends RuntimeException +{ + private static final long serialVersionUID = -3602596752342902060L; + + public QueryException() + { + } + + public QueryException( final String message ) + { + super( message ); + } + + public QueryException( final String message, final Throwable cause ) + { + super( message, cause ); + } +} http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/QueryExecutionException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/QueryExecutionException.java b/core/api/src/main/java/org/apache/zest/api/query/QueryExecutionException.java new file mode 100644 index 0000000..f068843 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/QueryExecutionException.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2008, Rickard Ãberg. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.zest.api.query; + +/** + * Throw this exception if a query could not be executed + */ +public final class QueryExecutionException + extends QueryException +{ + private static final long serialVersionUID = 5147421865890379209L; + + public QueryExecutionException( String message ) + { + super( message ); + } + + public QueryExecutionException( String message, Throwable cause ) + { + super( message, cause ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/zest-java/blob/061ddaa0/core/api/src/main/java/org/apache/zest/api/query/QueryExpressionException.java ---------------------------------------------------------------------- diff --git a/core/api/src/main/java/org/apache/zest/api/query/QueryExpressionException.java b/core/api/src/main/java/org/apache/zest/api/query/QueryExpressionException.java new file mode 100644 index 0000000..676bed0 --- /dev/null +++ b/core/api/src/main/java/org/apache/zest/api/query/QueryExpressionException.java @@ -0,0 +1,34 @@ +/* + * Copyright 2009 Niclas Hedhman. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zest.api.query; + +/** + * Throw this exception if a QueryExpression is invalid. + */ +public class QueryExpressionException + extends QueryException +{ + + private static final long serialVersionUID = 1L; + + public QueryExpressionException( String message ) + { + super( message ); + } + +}
