001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.csv;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.lang.reflect.*;
022
023import org.apache.juneau.commons.*;
024import org.apache.juneau.commons.reflect.*;
025import org.apache.juneau.commons.svl.*;
026import org.apache.juneau.marshall.*;
027
028/**
029 * Utility classes and methods for the {@link CsvApply @CsvApply} annotation.
030 *
031 */
032public class CsvApplyAnnotation {
033
034   private CsvApplyAnnotation() {}
035
036   @SuppressWarnings({
037      "rawtypes" // Raw types required for reflective annotation application.
038   })
039   public static class Applier extends AnnotationApplier<CsvApply,Context.Builder> {
040
041      public Applier(VarResolverSession vr) {
042         super(CsvApply.class, Context.Builder.class, vr);
043      }
044
045      @Override
046      public void apply(AnnotationInfo<CsvApply> ai, Context.Builder b) {
047         CsvApply a = ai.inner();
048         if (isEmptyArray(a.on()) && isEmptyArray(a.onClass()))
049            return;
050         b.annotations(a);
051      }
052   }
053
054   public static class Builder extends AppliedAnnotationObject.BuilderTMF {
055
056      Csv value = CsvAnnotation.DEFAULT;
057
058      protected Builder() {
059         super(CsvApply.class);
060      }
061
062      public Builder value(Csv value) {
063         this.value = value;
064         return this;
065      }
066
067      @Override public Builder on(String...value) { super.on(value); return this; }
068      @Override public Builder on(Class<?>...value) { super.on(value); return this; }
069      @Override public Builder onClass(Class<?>...value) { super.onClass(value); return this; }
070      @Override public Builder on(Method...value) { super.on(value); return this; }
071      @Override public Builder on(Field...value) { super.on(value); return this; }
072      @Override public Builder on(ClassInfo...value) { super.on(value); return this; }
073      @Override public Builder onClass(ClassInfo...value) { super.onClass(value); return this; }
074      @Override public Builder on(FieldInfo...value) { super.on(value); return this; }
075      @Override public Builder on(MethodInfo...value) { super.on(value); return this; }
076
077      public CsvApply build() {
078         return new Object(this);
079      }
080   }
081
082   @SuppressWarnings({
083      "java:S2160" // equals() inherited from AnnotationObject compares all annotation interface methods; subclass fields are accessed via those methods
084   })
085   private static class Object extends AppliedOnClassAnnotationObject implements CsvApply {
086
087      private final Csv value;
088
089      Object(CsvApplyAnnotation.Builder b) {
090         super(b);
091         value = b.value;
092      }
093
094      @Override public Csv value() { return value; }
095   }
096
097   public static final CsvApply DEFAULT = create().build();
098
099   public static Builder create() { return new Builder(); }
100   public static Builder create(Class<?>...on) { return create().on(on); }
101   public static Builder create(String...on) { return create().on(on); }
102
103   public static boolean empty(CsvApply a) {
104      return a == null || DEFAULT.equals(a);
105   }
106}