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