Example usage for java.util Vector get

List of usage examples for java.util Vector get

Introduction

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

Prototype

public synchronized E get(int index) 

Source Link

Document

Returns the element at the specified position in this Vector.

Usage

From source file:edu.umn.cs.spatialHadoop.util.Parallel.java

public static <T> List<T> forEach(int start, int end, RunnableRange<T> r, int parallelism)
        throws InterruptedException {
    Vector<T> results = new Vector<T>();
    if (end <= start)
        return results;
    final Vector<Throwable> exceptions = new Vector<Throwable>();
    Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread th, Throwable ex) {
            exceptions.add(ex);//from   w w  w .ja v  a  2s  . c  o  m
        }
    };

    // Put an upper bound on parallelism to avoid empty ranges
    if (parallelism > (end - start))
        parallelism = end - start;
    if (parallelism == 1) {
        // Avoid creating threads
        results.add(r.run(start, end));
    } else {
        LOG.info("Creating " + parallelism + " threads");
        final int[] partitions = new int[parallelism + 1];
        for (int i_thread = 0; i_thread <= parallelism; i_thread++)
            partitions[i_thread] = i_thread * (end - start) / parallelism + start;
        final Vector<RunnableRangeThread<T>> threads = new Vector<RunnableRangeThread<T>>();
        for (int i_thread = 0; i_thread < parallelism; i_thread++) {
            RunnableRangeThread<T> thread = new RunnableRangeThread<T>(r, partitions[i_thread],
                    partitions[i_thread + 1]);
            thread.setUncaughtExceptionHandler(h);
            threads.add(thread);
            threads.lastElement().start();
        }
        for (int i_thread = 0; i_thread < parallelism; i_thread++) {
            threads.get(i_thread).join();
            results.add(threads.get(i_thread).getResult());
        }
        if (!exceptions.isEmpty())
            throw new RuntimeException(exceptions.size() + " unhandled exceptions", exceptions.firstElement());
    }
    return results;
}

From source file:edu.ku.brc.dbsupport.DatabaseDriverInfo.java

/**
 * Returns a driver by name from the list of drivers.
 * @param drvName the name of the driver
 * @param dbDrivers the driver list/*from  w ww . j a v a  2s.  c  o  m*/
 * @return the driver info
 */
public static DatabaseDriverInfo getDriver(final String drvName) {
    Vector<DatabaseDriverInfo> list = getDriversList();
    int inx = -1;
    for (DatabaseDriverInfo ddi : list) {
        inx++;
        if ((ddi.getDriverClassName() != null && ddi.getDriverClassName().equals(drvName))
                || ddi.getName().equals(drvName)) {
            return list.get(inx);
        }
    }
    return null;
}

From source file:it.geosolutions.geobatch.destination.common.utils.RemoteBrowserUtils.java

/**
 * SFTP protocol ls/*from  w  w w.j a  va2  s .c o m*/
 * 
 * @param userName
 * @param password
 * @param host
 * @param port
 * @param path
 * @param timeout
 * @param pattern
 * @return name of files or directories inside the remote folder
 * @throws IOException
 * @throws FTPException
 * @throws ParseException
 */
private static List<String> sftpLS(String userName, String password, String host, int port, String path,
        int timeout, Pattern pattern) throws IOException {

    //TODO: use pattern
    Session session = null;
    Channel channel = null;
    ChannelSftp sftpChannel = null;
    List<String> names = new LinkedList<String>();

    try {
        JSch jsch = new JSch();
        session = jsch.getSession(userName, host, port);
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel(RemoteBrowserProtocol.sftp.name());
        channel.connect();
        sftpChannel = (ChannelSftp) channel;
        sftpChannel.cd(path);
        @SuppressWarnings("unchecked")
        Vector<LsEntry> filelist = sftpChannel.ls(path);
        for (int i = 0; i < filelist.size(); i++) {
            String fileName = filelist.get(i).getFilename();
            if (pattern != null) {
                Matcher m = pattern.matcher(fileName);
                if (m.matches()) {
                    names.add(fileName);
                }
            } else {
                if (!fileName.equals(".") && !fileName.equals("..")) {
                    names.add(fileName);
                }
            }
        }

    } catch (Exception e) {
        throw new IOException("Error on ls", e);
    } finally {
        if (sftpChannel != null) {
            sftpChannel.exit();
        }
        if (session != null) {
            session.disconnect();
        }
    }

    return names;
}

From source file:edu.ku.brc.af.tasks.StatsTrackerTask.java

/**
 * Builds NamePair array from list./*from w  w  w  .j ava2  s.c  o  m*/
 * @param postParams the list 
 * @return the array
 */
public static NameValuePair[] buildNamePairArray(final Vector<NameValuePair> postParams) {
    // create an array from the params
    NameValuePair[] paramArray = new NameValuePair[postParams.size()];
    for (int i = 0; i < paramArray.length; ++i) {
        paramArray[i] = postParams.get(i);
    }
    return paramArray;
}

From source file:org.sandrob.android.net.http.HttpsConnection.java

/**
* Gets the common name from the given X509Name.
* 
* @param name the X.509 name//  ww  w  .j  av a 2  s  .c  o  m
* @return the common name, null if not found
*/
private static String getCommonName(X509Name name) {
    if (name == null) {
        return null;
    }

    Vector<?> values = name.getValues(X509Name.CN);
    if (values == null || values.isEmpty()) {
        return null;
    }

    return values.get(0).toString();
}

From source file:com.legstar.codegen.tasks.SourceToXsdCobolTask.java

/**
 * Converts a URI into a package name. We assume a hierarchical,
 * server-based URI with the following syntax:
 * [scheme:][//host[:port]][path][?query][#fragment]
 * The package name is derived from host, path and fragment.
 * //w ww  .j a  v  a 2 s.co  m
 * @param namespaceURI the input namespace URI
 * @return the result package name
 */
public static String packageFromURI(final URI namespaceURI) {

    StringBuilder result = new StringBuilder();
    URI nURI = namespaceURI.normalize();
    boolean firstToken = true;

    /*
     * First part of package name is built from host with tokens in
     * reverse order.
     */
    if (nURI.getHost() != null && nURI.getHost().length() != 0) {
        Vector<String> v = new Vector<String>();
        StringTokenizer t = new StringTokenizer(nURI.getHost(), ".");
        while (t.hasMoreTokens()) {
            v.addElement(t.nextToken());
        }

        for (int i = v.size(); i > 0; i--) {
            if (!firstToken) {
                result.append('.');
            } else {
                firstToken = false;
            }
            result.append(v.get(i - 1));
        }
    }

    /* Next part of package is built from the path tokens */
    if (nURI.getPath() != null && nURI.getPath().length() != 0) {
        Vector<String> v = new Vector<String>();
        StringTokenizer t = new StringTokenizer(nURI.getPath(), "/");
        while (t.hasMoreTokens()) {
            v.addElement(t.nextToken());
        }

        for (int i = 0; i < v.size(); i++) {
            String token = v.get(i);
            /* ignore situations such as /./../ */
            if (token.equals(".") || token.equals("..")) {
                continue;
            }
            if (!firstToken) {
                result.append('.');
            } else {
                firstToken = false;
            }
            result.append(v.get(i));
        }
    }

    /* Finally append any fragment */
    if (nURI.getFragment() != null && nURI.getFragment().length() != 0) {
        if (!firstToken) {
            result.append('.');
        } else {
            firstToken = false;
        }
        result.append(nURI.getFragment());
    }

    /*
     * By convention, namespaces are lowercase and should not contain
     * invalid Java identifiers
     */
    String s = result.toString().toLowerCase();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        Character c = s.charAt(i);
        if (Character.isJavaIdentifierPart(c) || c.equals('.')) {
            sb.append(c);
        } else {
            sb.append("_");
        }
    }
    return sb.toString();
}

From source file:AnimatedMetadataGraph.java

public static String getParentPath(String childPath) {
    if (!childPath.equals("/")) {
        Vector<String> pathElements = new Vector<String>();
        StringTokenizer tokenizer = new StringTokenizer(childPath, "/");
        while (tokenizer.hasMoreTokens())
            pathElements.add(tokenizer.nextToken());

        StringBuffer parentPath = new StringBuffer().append("/");
        for (int i = 0; i < pathElements.size() - 1; i++)
            parentPath.append(pathElements.get(i)).append("/");
        return parentPath.toString();
    }//from w w w . j a va 2s .  c  o  m
    return null;

}

From source file:common.email.MailServiceImpl.java

/**
 * this method loads email template into StringBuilder
 * this method sends email using a given template
 * @param params names of parameters to replace in template
 * @param values values by whitch to replace names
 * @param pathToTemplate path to template for email message
* @return stringBuilder this email//from   w  w w  .  j a v  a 2s .  c om
* @throws java.io.FileNotFoundException
 * @throws java.io.IOException
 */
protected static StringBuilder getMailMsg(Vector<String> params, Vector<String> values, String pathToTemplate)
        throws FileNotFoundException, IOException {
    String line = "";
    StringBuilder rez = new StringBuilder();
    if (params != null && values != null) {
        BufferedReader read = new BufferedReader(
                new InputStreamReader(new FileInputStream(pathToTemplate), "UTF-8"));
        while ((line = read.readLine()) != null) {
            for (int i = 0; i < params.size(); i++) {
                line = line.replaceAll(params.get(i), values.get(i));
            }
            rez.append(line);
            rez.append("\n");
        }
        read.close();
    } else {
        BufferedReader read = new BufferedReader(
                new InputStreamReader(new FileInputStream(pathToTemplate), "UTF-8"));
        while ((line = read.readLine()) != null) {
            rez.append(line);
            rez.append("\n");
        }
        read.close();
    }
    return rez;
}

From source file:modnlp.capte.AlignerUtils.java

public static void reWriteAlignment(String sourcename, String targetname, Vector<Object[]> d) {

    try {//w ww  .ja va  2 s . c om
        FileOutputStream op = new FileOutputStream(sourcename);
        Writer out = new OutputStreamWriter(op, "UTF-8");
        FileOutputStream sp = new FileOutputStream(targetname);
        Writer sout = new OutputStreamWriter(sp, "UTF-8");
        String source = "";
        String target = "";
        Object[] stemp;
        for (int i = 0; i < d.size(); i++) {
            stemp = d.get(i);
            source = (String) stemp[0];
            target = (String) stemp[1];
            source = clean(source);
            target = clean(target);
            out.write(source);
            out.write("\n");
            sout.write(target);
            sout.write("\n");
            System.out.println(source);
            System.out.println(target);

        }
        out.close();
        sout.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip.//  w  ww  . j  av  a2  s. c o  m
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}