Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright 2013 Jeanfrancois Arcand
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
   * use this file except in compliance with the License. You may obtain a copy of
   * the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations under
  * the License.
 package org.atmosphere.wasync;

A decoder can be used to 'decode' the response's body. Decoder's can be used to decode the response body and match them with Function's implementation. This library will try to match the decoded Object to its associated Function. For example, the Decoder's type will be mapped, by the FunctionResolver to the Function of the same type if no function message has been defined:

Decoder<String, POJO> d = new Decoder<String, POJO>() {
public POJO decode(Event e, String s) {
return new POJO(s);
Important: Decoder cannot be chained, e.g the result of a Decoder will never be passed to another Decoder, like Encoder

Jeanfrancois Arcand
 public interface Decoder<U, T> {
Decode the specified object of type U into object of type T.

e an Event type. This can be used to differentiate event received.
s a object of type U
a new object of type T
     T decode(Event e, U s);

A Decoder may return an instance of a Decoded object to prevent some messages from being delivered to a Function, by returning a ABORT instance. Returning an instance of Decoded with the action set to Decoder.Decoded.ACTION.CONTINUE will allow dispatching messages to Function

     public final static class Decoded<T> {
Use this object to prevent the delivering of messages to Function
         public final static Decoded ABORT = new Decoded(null.);
         public enum ACTION {
Continue the decoding/dispatching process.
Do not dispatch the message to Function
         private final T decodedMessage;
         private final ACTION action;

Create a decoded object

decodedMessage the decoded message
action the action
         public Decoded(T decodedMessageACTION action) {
             this. = decodedMessage;
             this. = action;

Create a decoded object with action set to Decoder.Decoded.ACTION.CONTINUE

         public Decoded(T decodedMessage) {
             this. = decodedMessage;
             this. = .;

Return the decoded message.

the decoded message.
         public T decoded() {
             return ;

Return the action.

the action.
        public ACTION action() {
            return ;
New to GrepCode? Check out our FAQ X