Multiple return values for Java processing methods

Source: Internet
Author: User

The first time I touched the tuple was in the Java Programming Mind book, when I was distressed by multiple return values for the method. Because I was dealing with multiple return value methods before, I would either build a new entity class, or create an object in the Received method, assign a value before the return value, or throw it up with the exception mechanism, either of which is cumbersome. But tuples are not the same, we just have to set up a good tuple class and tool classes can be used anywhere, and simple, clear.

/***
* Function Description: Login
* @author Cheng 2016-5-26 03:21:30
* @param loginName User Name
* @param apptype User Type 1 student other teachers
* @param loginpass Login Password
* @return
*/
Public twotuple<integer,tuser> SignIn (String loginname,integer apptype,string loginpass) {
if (Emptyutils.isempty (loginName) | | Emptyutils.isempty (Apptype))
Return Tupleutil.tuple (0,null);//Parameter error
TUser User=get (Tuser.class, "LoginName", loginName);
if (apptype==1) {
if (User.getusertype ()!=1)
Return Tupleutil.tuple ( -1,null);
}else{
if (User.getusertype () ==1)
Return Tupleutil.tuple ( -1,null);
}
if (emptyutils.isempty (user))
Return Tupleutil.tuple ( -1,null);//user name does not exist
if (!user.getloginpass (). Equals (Loginpass))
Return Tupleutil.tuple ( -2,null);//Password error
Return Tupleutil.tuple (1,user);
}

/***
* Function Description: Login
* @author Cheng 2016-5-4 11:43:38
* @param loginName
* @param loginpass
* @return
*/
Public Appcommonmodel SignIn (String loginname,string loginpass,integer apptype) {
Twotuple<integer,tuser> twotule= Userservice.signin (loginname,apptype,loginpass);
String msg= "";
Switch (Twotule.first) {
Case 0:
msg= "parameter cannot be empty";
Break
Case-2:
msg= "Password Error";
Break
Case-1:
msg= "User name does not exist";
Break

Default
return new Appcommonmodel (Twotule.first, "Login Successful", Twotule.second);
}
return new Appcommonmodel (Appcommonmodel.error,msg, "{}");
}

This is a login interface I write with a tuple, the idea is simple, but to return two parameters, using tuple post-processing has become much simpler.

Defining tuples

public class Twotuple<a, b> {

Public final A first;
Public final B second;

Public Twotuple (a A, b b) {
This.first = A;
This.second = b;
}

}

Tuple Tool Class

public class Tupleutil {
/***
* Function Description: two-dimensional tuple
* @author Cheng 2016-5-24 09:35:38
* @param <A>
* @param <B>
* @param a
* @param b
* @return
*/
public static <a, b> Twotuple<a, b> tuple (a A, b b) {
return new Twotuple <a, b> (A, B);
}

}

Whether you define a tuple or a tuple tool class is concise, and it is very simple to use.

Here I want to explain that the tuple is defined with public final and only constructed, so it can only be assigned when it is created, and cannot be assigned to it in any other way, so don't worry about it being insecure.

Multiple return values for Java processing methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.