001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.cmdline;
021    
022    import org.crsh.cmdline.annotations.Man;
023    import org.crsh.cmdline.annotations.Usage;
024    
025    import java.lang.annotation.Annotation;
026    import java.lang.reflect.AnnotatedElement;
027    
028    public final class Description {
029    
030      /** . */
031      private final String usage;
032    
033      /** . */
034      private final String man;
035    
036      public Description() {
037        this.usage = this.man = "";
038      }
039    
040      Description(Description child, Description parent) {
041        if (child == null) {
042          throw new NullPointerException();
043        }
044        if (parent == null) {
045          throw new NullPointerException();
046        }
047    
048        //
049        this.usage = child.usage.length() > 0 ? child.usage : parent.usage;
050        this.man = child.man.length() > 0 ? child.man : parent.man;
051      }
052    
053      Description(String usage, String man) {
054        if (usage == null) {
055          throw new NullPointerException();
056        }
057        if (man == null) {
058          throw new NullPointerException();
059        }
060    
061        //
062        this.usage = usage;
063        this.man = man;
064      }
065    
066      public Description(AnnotatedElement annotated) {
067        this(annotated.getAnnotations());
068      }
069    
070      Description(Annotation... annotations) {
071        if (annotations == null) {
072          throw new NullPointerException();
073        }
074    
075        //
076        String usage = "";
077        String man = "";
078        for (Annotation annotation : annotations) {
079          if (annotation instanceof Usage) {
080            usage = ((Usage)annotation).value();
081          } else if (annotation instanceof Man) {
082            man = ((Man)annotation).value();
083          }
084        }
085    
086        //
087        this.usage  = usage;
088        this.man  = man;
089      }
090    
091      public String getUsage() {
092        return usage;
093      }
094    
095      public String getMan() {
096        return man;
097      }
098    
099      String getBestEffortMan() {
100        if (man.length() > 0) {
101          return man;
102        } else {
103          return usage;
104        }
105      }
106    
107      @Override
108      public boolean equals(Object obj) {
109        if (obj == this) {
110          return true;
111        } else if (obj instanceof Description) {
112          Description that = (Description)obj;
113          return usage.equals(that.usage) && man.equals(that.man);
114        } else {
115          return false;
116        }
117      }
118    }