-2
 public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

what is done in this second line? I can't understand what is this (tag) ?TAG :tag.

Here is the full code

{
import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

here is the full code now tell me and please explain if you can that what is done in that method second line

Suparna
  • 1,132
  • 1
  • 8
  • 20
  • How will we know? It's your code. Looks like some instance variable of class Request. Please provide complete context if it is generic enough. – Aniket Thakur Mar 26 '16 at 14:30
  • Are you asking how the ?: operator works? – John Hascall Mar 26 '16 at 14:43
  • yes how the operator working and what the code of second line of that method done – Mian Hassan Mar 26 '16 at 14:47
  • An expression "b ? v1 : v2" evaluates the boolean "b" and if it is true then the expression has value v1 else it has the value v2. Kind of like "int questionmark ( boolean b, int v1, int v2 ) { if (b) return v1; return v2; } but it's not limited to int's -- v1/v2 can be any type. – John Hascall Mar 26 '16 at 14:53
  • thanks John ...thanks for such a concept really appreciate your knowledge – Mian Hassan Mar 26 '16 at 16:15
  • Possible duplicate of [What is the Java ?: operator called and what does it do?](https://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do) – domsson Jul 24 '17 at 12:46

2 Answers2

0

TAG must be some constant of type String representing the default value of tag, whatever it is - something like this:

public static final String TAG = "<default-tag>";

The idea here is to tag your request with the value passed as the second parameter, i.e. String tag. Conditional operator ?: is used to implement the check without using an if/else statement.

When the tag string passed in the call to addToRequestQueue happens to be empty, the method uses the value of TAG instead.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Please also add that this is the ternary or inline if operator. And a link explaining that. – Nic Mar 26 '16 at 14:53
0

Translation of the second line

public static final String TAG = AppController.class.getSimpleName();

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag("is tag empty?" yes, so use `TAG` : no, so use `tag`);
    getRequestQueue().add(req);
} 

In another words:

if(tag.isEmpty()){
  res.setTag(TAG)

} else {
  res.setTag(tag)

}
Andre Haueisen
  • 468
  • 2
  • 8
  • 20