001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program 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
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/randommedia/CacheObject.java,v 1.5 2004/05/21 07:28:54 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.randommedia;
021    
022    import java.net.URL;
023    import org.apache.log4j.*;
024    
025    /**
026     * A (media) object that can be cached.
027     *
028     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
029     * @version $Revision: 1.5 $
030     */
031    public class CacheObject
032    {
033      /**
034       * The URL of this object.
035       *
036       */
037      public volatile URL url;
038      /**
039       * The object itself.
040       *
041       */
042      public volatile Object object;
043      /**
044       * The type (i.e., image, sound, video clip) of object.
045       *
046       */
047      private volatile int type; // implement later
048      static int count = 0;
049    
050        /**
051         * Creates a new <code>CacheObject</code> instance.
052         *
053         * @param myUrl an <code>URL</code> value
054         * @param myObj an <code>Object</code> value
055         */
056        public CacheObject(URL myUrl,Object myObj)
057        {
058            url = myUrl;
059            object = myObj;
060            type = -1;
061            count++;
062        }
063        
064      /**
065       * Returns the object's filename.
066       *
067       * @return a <code>String</code> value
068       */
069      public String getFileName()
070      {
071              if(url==null){
072                      return null;
073              }
074              return url.toString();
075      }
076    
077    
078      /**
079       * The substring after the last slash in the filename.
080       * I.e., file://foo/bar/whee/whopper.jpg -> whopper.jpg.
081       *
082       * @return a <code>String</code> value
083       */
084      public String getFileShortName()
085      {
086          // FIXME: this should be doable with File.something()
087         if(url==null){
088            return null;
089         }
090        String la = url.toString();
091        int foo = la.lastIndexOf("/");
092        if(foo==-1){
093          return la;
094        }
095        return la.substring(foo+1);
096      }
097    
098      /**
099       * What's before the last slash in the filename.
100       * I.e., file://foo/bar/whee/whopper.jpg -> file://foo/bar/whee.
101       *
102       * @return a <code>String</code> value
103       */
104      public String getDirectoryName()
105      {
106          // FIXME: this should be doable with File.something()
107         if(url==null){
108            return null;
109        }
110        String la = url.toString();
111        int foo = la.lastIndexOf("/");
112        if(foo==-1){
113                return la;
114        }
115        return la.substring(0,foo-1);
116      }
117    
118        /**
119         * Describe <code>finalize</code> method here.
120         *
121         * @exception Throwable if an error occurs
122         */
123        protected void finalize() throws Throwable
124      {
125          count--;
126          super.finalize();
127      }
128    
129    
130    
131    }