View Javadoc

1   package org.sonatype.aether.util.listener;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  import java.nio.ByteBuffer;
12  
13  import org.sonatype.aether.transfer.TransferEvent;
14  import org.sonatype.aether.transfer.TransferResource;
15  
16  /**
17   * A simple transfer event.
18   * 
19   * @author Benjamin Bentmann
20   */
21  public class DefaultTransferEvent
22      implements TransferEvent
23  {
24  
25      private EventType type = EventType.INITIATED;
26  
27      private RequestType requestType = RequestType.GET;
28  
29      private TransferResource resource;
30  
31      private ByteBuffer dataBuffer;
32  
33      private long transferredBytes;
34  
35      private Exception exception;
36  
37      /**
38       * Creates a new and uninitialized transfer event. Use the various setters to populate the event.
39       */
40      public DefaultTransferEvent()
41      {
42      }
43  
44      public EventType getType()
45      {
46          return type;
47      }
48  
49      /**
50       * Sets the type of the event.
51       * 
52       * @param type The type of the event, must not be {@code null}.
53       * @return This event for chaining, never {@code null}.
54       */
55      public DefaultTransferEvent setType( EventType type )
56      {
57          if ( type == null )
58          {
59              throw new IllegalArgumentException( "event type not specified" );
60          }
61          this.type = type;
62          return this;
63      }
64  
65      public RequestType getRequestType()
66      {
67          return requestType;
68      }
69  
70      /**
71       * Sets the type of the request/transfer.
72       * 
73       * @param requestType The request/transfer type, must not be {@code null}.
74       * @return This event for chaining, never {@code null}.
75       */
76      public DefaultTransferEvent setRequestType( RequestType requestType )
77      {
78          if ( requestType == null )
79          {
80              throw new IllegalArgumentException( "request type not specified" );
81          }
82          this.requestType = requestType;
83          return this;
84      }
85  
86      public TransferResource getResource()
87      {
88          return resource;
89      }
90  
91      /**
92       * Sets the resource being transferred.
93       * 
94       * @param resource The resource being transferred, must not be {@code null}.
95       * @return This event for chaining, never {@code null}.
96       */
97      public DefaultTransferEvent setResource( TransferResource resource )
98      {
99          if ( resource == null )
100         {
101             throw new IllegalArgumentException( "transfer resource not specified" );
102         }
103         this.resource = resource;
104         return this;
105     }
106 
107     public long getTransferredBytes()
108     {
109         return transferredBytes;
110     }
111 
112     /**
113      * Sets the total number of bytes that have been transferred so far during the download/upload.
114      * 
115      * @param transferredBytes The total number of bytes that have been transferred so far during the download/upload,
116      *            must not be negative.
117      * @return This event for chaining, never {@code null}.
118      */
119     public DefaultTransferEvent setTransferredBytes( long transferredBytes )
120     {
121         if ( transferredBytes < 0 )
122         {
123             throw new IllegalArgumentException( "number of transferred bytes cannot be negative" );
124         }
125         this.transferredBytes = transferredBytes;
126         return this;
127     }
128 
129     public int getDataLength()
130     {
131         return ( dataBuffer != null ) ? dataBuffer.remaining() : 0;
132     }
133 
134     public ByteBuffer getDataBuffer()
135     {
136         return ( dataBuffer != null ) ? dataBuffer.asReadOnlyBuffer() : null;
137     }
138 
139     /**
140      * Wraps the given <code>byte[]</code>-array into a {@link ByteBuffer} as the content for this event.
141      * 
142      * @param buffer The array to use, must not be {@code null}.
143      * @param offset the starting point of valid bytes in the array.
144      * @param length the number of valid bytes.
145      * @return This event for chaining, never {@code null}.
146      */
147     public DefaultTransferEvent setDataBuffer( byte[] buffer, int offset, int length )
148     {
149         return setDataBuffer( ByteBuffer.wrap( buffer, offset, length ) );
150     }
151 
152     /**
153      * Sets the byte buffer holding the transferred bytes since the last event.
154      * 
155      * @param dataBuffer The byte buffer holding the transferred bytes since the last event, may be {@code null}.
156      * @return This event for chaining, never {@code null}.
157      */
158     public DefaultTransferEvent setDataBuffer( ByteBuffer dataBuffer )
159     {
160         this.dataBuffer = dataBuffer;
161         return this;
162     }
163 
164     public Exception getException()
165     {
166         return exception;
167     }
168 
169     /**
170      * Sets the error that occurred during the transfer.
171      * 
172      * @param exception The error that occurred during the transfer, may be {@code null} if none.
173      * @return This event for chaining, never {@code null}.
174      */
175     public DefaultTransferEvent setException( Exception exception )
176     {
177         this.exception = exception;
178         return this;
179     }
180 
181     @Override
182     public String toString()
183     {
184         return getRequestType() + " " + getType() + " " + getResource();
185     }
186 
187 }