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
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * 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;

FunctionResolver are useful for mapping received message with a Function.
Client client = ClientFactory.getDefault().newClient();

RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(targetUrl + "/suspend")
.decoder(new Decoder<String, POJO>() {
@Override
public POJO decode(String s) {
return new POJO(s);
}
})
.transport(Request.TRANSPORT.WEBSOCKET);

Socket socket = client.create();
socket.on(Event.MESSAGE, new Function<POJO>() {
@Override
public void on(POJO t) {
response.set(t);
}
})
 
or when a Decoder share the same type as the defined Function. For example:
Client client = ClientFactory.getDefault().newClient();

RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(targetUrl + "/suspend")
.decoder(new Decoder<String, POJO>() {
@Override
public POJO decode(String s) {
return new POJO(s);
}
})
.transport(Request.TRANSPORT.WEBSOCKET);

Socket socket = client.create();
socket.on(new Function<POJO>() {
@Override
public void on(POJO t) {
response.set(t);
}
})
 
An application can define its own Decoder to Function Mapper be writing the appropriate FunctionResolver. By default, the DEFAULT is used.

Author(s):
Jeanfrancois Arcand
 
 public interface FunctionResolver {
 
     FunctionResolver DEFAULT = new FunctionResolver() {
        
 
         @Override
         public boolean resolve(String messageObject functionNameFunctionWrapper fn) {
             return (fn.functionName().length() == 0 && !find(message)) || functionName.toString().equalsIgnoreCase(fn.functionName());
         }

        
Discard empty function name fired by the library

Parameters:
functionName
Returns:
 
         private boolean find(String functionName) {
             for (Event e: Event.values()) {
                 if (e.name().equalsIgnoreCase(functionName)) {
                     return true;
                 }
             }
             return false;
         }
     };

    
Resolve the current message

Parameters:
message the original response's body received
functionName the default function name taken from Event or from a custom FunctionResolver
fn The current FunctionWrapper
Returns:
true if the Function can be invoked.
    boolean resolve(String messageObject functionNameFunctionWrapper fn);
New to GrepCode? Check out our FAQ X