把部分返回null的方法放在其他方法中

时间:2015-05-05 08:30:44

标签: java

方法storeData工作正常,但我试图将我的方法的一部分放在另一个方法samePosition中以最小化它,但我有一个问题,它的一部分返回{{1 }}

我把它放在其他方法名null中,返回值为samePosition,但之后我收到了以下错误消息:

  

Integer

我该如何解决这个问题?

最小化前的

This method must return a result of type Integer方法:

storeData

修改

storeData:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response storeData(Data data) {

    String macD = data.getMac();
    int routeD = data.getRoute();
    float latD = data.getLatitude();
    float longD = data.getLongitude();

    // Add the lat and Long to the ArrayList.
    latLongList.add(new LatLong(latD, longD));

////////////I want to put this part in seperate method "samePosition" //////////////
    int size = latLongList.size();
    if (size > 1) {
        ArrayList<Double> array = new ArrayList<>();

        for (int i = 0; i < latLongList.size() - 1; i++) {
            LatLong last = latLongList.get(latLongList.size() - 1);

            double distance = haversineDistance(latLongList.get(i), last);
            array.add(distance);

        }

        ArrayList<Double> distanceRange = new ArrayList<>();

        for (int j = 0; j < array.size(); j++) {

            // Store request in the circumcircle of 4 meter into ArrayList.
            if (array.get(j) < 4) {

                distanceRange.add(array.get(j));
            }
        }

        if (distanceRange.size() == 0) {
            processData(macD, routeD, latD, longD);
        } else {
            return null;
        }

    } else {

        processData(macD, routeD, latD, longD);
    }
//////////////////////until here/////////////////////////
    return Response.status(201).build();
}

samePosition方法:

public Response storeData(Data data) {

        String macD = data.getMac();
        int routeD = data.getRoute();
        float latD = data.getLatitude();
        float longD = data.getLongitude();

        // Add the lat and Long to the ArrayList.
        latLongList.add(new LatLong(latD, longD));

        System.out.println("latLondList size: " + latLongList.size());

         int valueReturned =   samePosition(macD, routeD, latD, longD);

        if (valueReturned == -1) {
            return null;
            } else {
                return Response.status(201).build();
            }   
    }

1 个答案:

答案 0 :(得分:0)

您可以返回-1,而不是返回null,然后检查返回的值为-1。

if (valueReturned == -1) {
return null;
} else {
    //continue with method
}

samePosition()方法应该始终返回一个整数:

private int samePosition(String macD, int routeD, float latD, float longD) {
    int size = latLongList.size();
    if (size > 1) {
        ArrayList<Double> array = new ArrayList<>();

        for (int i = 0; i < latLongList.size() - 1; i++) {
            LatLong last = latLongList.get(latLongList.size() - 1);

            double distance = haversineDistance(latLongList.get(i), last);
            array.add(distance);

        }

        ArrayList<Double> distanceRange = new ArrayList<>();

        for (int j = 0; j < array.size(); j++) {

            // Store request in the circumcircle of 4 meter into ArrayList.
            if (array.get(j) < 4) {

                distanceRange.add(array.get(j));
            }
        }

        if (distanceRange.size() == 0) {
            processData(macD, routeD, latD, longD);
            return 0;
        } else {
            return -1;
        }

    } else {
        processData(macD, routeD, latD, longD);
            return 0;
    }
}