Example usage for java.util Vector set

List of usage examples for java.util Vector set

Introduction

In this page you can find the example usage for java.util Vector set.

Prototype

public synchronized E set(int index, E element) 

Source Link

Document

Replaces the element at the specified position in this Vector with the specified element.

Usage

From source file:Main.java

/**
 * Method sortVectorRecursive.//from w  w  w  .j  a va2 s. co  m
 * @param vector Vector
 * @param getter String
 * @param low double
 * @param high double
 * @param sortDirection int
 * @param caseSensitiveSort boolean
 * @throws Exception
 */
private static void sortVectorRecursive(Vector vector, String getter, double low, double high,
        int sortDirection, boolean caseSensitiveSort) throws Exception {
    Class[] cls = new Class[0];
    Object[] objs = new Object[0];
    Object middleVectorElem = vector.elementAt((int) ((low + high) / 2));
    Method method = null;
    Object middleObject = null;

    if (middleVectorElem != null) {
        method = middleVectorElem.getClass().getMethod(getter, cls);
        middleObject = method.invoke(middleVectorElem, objs);
    }

    Object tmpElement;
    Object compareVectorElem;
    Object compareObject;
    int i = (int) high;
    int j = (int) low;

    Class[] objectClass = new Class[1];
    objectClass[0] = Class.forName("java.lang.Object");
    Class[] stringObjectClass = new Class[1];
    stringObjectClass[0] = Class.forName("java.lang.String");

    Object[] object = new Object[1];
    object[0] = middleObject;
    Object compResult;
    boolean repeatDo;

    do {
        do {
            compareVectorElem = vector.elementAt(j);
            if (compareVectorElem != null) {
                method = compareVectorElem.getClass().getMethod(getter, cls);
                compareObject = method.invoke(compareVectorElem, objs);
                if (compareObject != null) {
                    if (!(caseSensitiveSort) && compareObject instanceof String
                            && object[0] instanceof String) {
                        method = compareObject.getClass().getMethod("compareToIgnoreCase", stringObjectClass);
                    } else {
                        method = compareObject.getClass().getMethod("compareTo", objectClass);
                    }
                    if (object[0] != null) {
                        compResult = method.invoke(compareObject, object);
                    } else {
                        compResult = new Integer(1);
                    }
                } else {
                    if (object[0] != null) {
                        compResult = new Integer(-1);
                    } else {
                        compResult = new Integer(0);
                    }
                }
            } else {
                if (object[0] != null) {
                    compResult = new Integer(-1);
                } else {
                    compResult = new Integer(0);
                }
            }

            if (((sortDirection == SORT_UP) && (((Integer) compResult).intValue() < 0))
                    || ((sortDirection == SORT_DOWN) && (((Integer) compResult).intValue() > 0))) {
                j++;
                repeatDo = true;
            } else {
                repeatDo = false;
            }
        } while (repeatDo);

        do {
            compareVectorElem = vector.elementAt(i);
            if (compareVectorElem != null) {
                method = compareVectorElem.getClass().getMethod(getter, cls);
                compareObject = method.invoke(compareVectorElem, objs);
                if (compareObject != null) {
                    if (!(caseSensitiveSort) && compareObject instanceof String
                            && object[0] instanceof String) {
                        method = compareObject.getClass().getMethod("compareToIgnoreCase", stringObjectClass);
                    } else {
                        method = compareObject.getClass().getMethod("compareTo", objectClass);
                    }

                    if (object[0] != null) {
                        compResult = method.invoke(compareObject, object);
                    } else {
                        compResult = new Integer(1);
                    }
                } else {
                    if (object[0] != null) {
                        compResult = new Integer(-1);
                    } else {
                        compResult = new Integer(0);
                    }
                }
            } else {
                if (object[0] != null) {
                    compResult = new Integer(-1);
                } else {
                    compResult = new Integer(0);
                }
            }
            if (((sortDirection == SORT_UP) && (((Integer) compResult).intValue() > 0))
                    || ((sortDirection == SORT_DOWN) && (((Integer) compResult).intValue() < 0))) {
                i--;
                repeatDo = true;
            } else {
                repeatDo = false;
            }
        } while (repeatDo);

        if (i >= j) {
            if (i != j) {
                tmpElement = vector.elementAt(i);

                vector.set(i, vector.elementAt(j));
                vector.set(j, tmpElement);
            }

            i--;
            j++;
        }
    } while (j <= i);

    if (low < i) {
        sortVectorRecursive(vector, getter, low, i, sortDirection, caseSensitiveSort);
    }

    if (j < high) {
        sortVectorRecursive(vector, getter, j, high, sortDirection, caseSensitiveSort);
    }
}

From source file:gate.creole.ontology.impl.sesame.OntologyServiceImplSesame.java

String executeQuery(String serqlQuery) {
    //logger.info("executeQuery: "+serqlQuery);
    TupleQueryResult res = null;/*from   ww  w  .  j  a va 2 s .  c o m*/
    String ret = "";
    String msg = "Error executing query: " + serqlQuery;
    try {
        res = repositoryConnection.prepareTupleQuery(org.openrdf.query.QueryLanguage.SERQL, serqlQuery)
                .evaluate();
        // TODO: convert to string that is compatible what the old Sesame1 to string
        // method did!
        // code taken from Sesame1    org.openrdf.sesame.query.QueryResultsTable.toString()
        StringBuffer buf = new StringBuffer();
        List<String> bindings = res.getBindingNames();
        //System.out.println("Found bindings: "+bindings);
        String[] _columnNames = bindings.toArray(new String[0]);
        //System.out.println("Found columns names: "+_columnNames);
        if (_columnNames != null) {
            for (int i = 0; i < _columnNames.length; i++) {
                if (i > 0) {
                    buf.append("\t| ");
                }
                buf.append(_columnNames[i]);
            }
            buf.append('\n');

            int dashCount = buf.length() + 7 * (_columnNames.length - 1);
            for (int i = 0; i < dashCount; i++) {
                buf.append('-');
            }
            buf.append('\n');
        }

        Vector<Value> columns = new Vector<Value>(bindings.size());
        for (int j = 0; j < bindings.size(); j++) {
            columns.add(null);
        }
        //for (int i = 0; i < _rowList.size(); i++) {
        while (res.hasNext()) {
            BindingSet bs = res.next();
            int i = 0;
            for (String name : bindings) {
                columns.set(i++, bs.getValue(name));
                //System.out.println("Found columns: "+columns);
            }
            //List columns = (List)_rowList.get(i);

            for (int j = 0; j < columns.size(); j++) {
                if (j > 0) {
                    buf.append("\t| ");
                }
                buf.append(columns.get(j).stringValue());
            }
            buf.append('\n');
        }

        ret = buf.toString();

    } catch (QueryEvaluationException ex) {
        throw new GateOntologyException(msg, ex);
    } catch (RepositoryException ex) {
        throw new GateOntologyException(msg, ex);
    } catch (MalformedQueryException ex) {
        throw new GateOntologyException(msg, ex);
    } finally {
        if (res != null) {
            try {
                res.close();
            } catch (QueryEvaluationException ex) {
                throw new GateOntologyException(msg, ex);
            }
        }
    }
    //logger.info("executeQuery returns:\n"+ret+"\n");
    return ret;
}

From source file:webServices.RestServiceImpl.java

@GET
@Path("/mapLayersInfo/{id}/{host}/{endpoint}/{qType}/{port}")
@Produces(MediaType.APPLICATION_XML)/*from www  . ja va 2  s . c o  m*/
public List<MapInfo> getMapLayers(@PathParam("id") String id, @PathParam("host") String host,
        @PathParam("endpoint") String endpoint, @PathParam("qType") String qType, @PathParam("port") int port)
        throws EndpointCommunicationException {

    //Reset to default registry values
    endpointStore = new MapEndpointStore();

    //Get host info if the map is not saved in Registry
    if (!host.equalsIgnoreCase("none")) {
        endpointStore.setEndpointQuery(host, port, endpoint + "/" + qType);
    }

    //query registry to get the layers of the map with the specific id
    Vector<Vector<String>> results = endpointStore.openMapFromLink(id);

    int chartPosition = -1;

    //Pose queries to endpoints to get the KML files
    for (int i = 0; i < results.size(); i++) {
        Vector<String> temp = results.get(i);

        //End this loop at chart info separator
        if (temp.get(0).equalsIgnoreCase("@@@")) {
            chartPosition = i + 1;
            break;
        }

        String kmlFile = null;
        String hostName = null;
        String endpointName = null;
        String[] parts = null;
        String[] hostArr = null;

        //If an endpointURI exists, get the host and the name of it         
        if (temp.get(3) != null) {
            parts = temp.get(3).split("/");
            hostName = parts[2];
            port = 80;
            hostArr = hostName.split(":");
            if (hostArr.length > 1) {
                port = Integer.parseInt(hostArr[1]);
            }

            endpointName = "";
            for (int b = 3; b < parts.length - 1; b++) {
                //endpointName = parts[3] + "/" + parts[4];   
                endpointName += parts[b] + "/";
            }
            endpointName += parts[parts.length - 1];
        }

        /**
         * Query the respective endpoint to get the .kml file
         */
        if (parts != null && temp.get(1) != null) {
            if (!hostName.equalsIgnoreCase("data.ordnancesurvey.co.uk")) {
                //Strabon endpoint
                try {
                    //System.out.println("*** Query: " + temp.get(1));
                    //System.out.println("*** LayerName: " + temp.get(0));
                    //System.out.println("*** Host: " + hostName);
                    //System.out.println("*** Port: " + port);
                    //System.out.println("*** EndpointName: " + endpointName);
                    kmlFile = passQuery(temp.get(1), temp.get(0), hostName, port, endpointName);
                    //kmlFile = passQuery(query, name, host, port, endpointName.replaceAll("@@@", "/"));

                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
            } else {
                //Ordnance Survey SPARQL endpoint
                try {
                    kmlFile = passQueryOS(temp.get(1), temp.get(0));
                } catch (RuntimeException e) {

                } catch (QueryResultParseException e) {
                    e.printStackTrace();
                } catch (TupleQueryResultHandlerException e) {
                    e.printStackTrace();
                } catch (QueryEvaluationException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            temp.set(2, kmlFile);
            results.set(i, temp);

        }
    }

    //Wrap the results 
    ArrayList<MapInfo> mapInformation = new ArrayList<MapInfo>();

    for (int i = 0; i < results.size(); i++) {
        MapInfo info = new MapInfo();

        Vector<String> temp = results.get(i);
        //End this loop at chart info separator
        if (temp.get(0).equalsIgnoreCase("@@@")) {
            break;
        }

        info.init(temp);
        mapInformation.add(info);
    }

    //Pose queries to endpoints to get chart results and wrap results
    if (chartPosition != -1) {
        for (int i = chartPosition; i < results.size(); i++) {
            Vector<String> temp = results.get(i);

            String hostName = null;
            String endpointName = "";
            String[] parts = null;
            String[] hostArr = null;

            //Get the host and the name of the endpoint      
            parts = temp.get(3).split("/");
            hostName = parts[2];
            port = 80;
            hostArr = hostName.split(":");
            if (hostArr.length > 1) {
                port = Integer.parseInt(hostArr[1]);
            }

            for (int j = 3; j < parts.length - 1; j++) {
                endpointName = endpointName.concat(parts[j]);
                endpointName = endpointName.concat("/");
            }
            endpointName = endpointName.concat(parts[parts.length - 1]);

            Vector<String> resultsChart = new Vector<String>();
            String format = "";

            resultsChart = endpointStore.getDataForChart(hostName, endpointName, port, temp.get(2));

            for (int j = 0; j < resultsChart.size(); j++) {
                format = format.concat(resultsChart.get(j));
                format = format.concat("$");
            }

            MapInfo info = new MapInfo("chart", temp.get(2), temp.get(3), temp.get(1), format, temp.get(4),
                    temp.get(5), temp.get(6));
            mapInformation.add(info);
        }
    }

    return mapInformation;
}