Example usage for java.util Vector removeAllElements

List of usage examples for java.util Vector removeAllElements

Introduction

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

Prototype

public synchronized void removeAllElements() 

Source Link

Document

Removes all components from this vector and sets its size to zero.

Usage

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.control.CallMarketEngine.java

/**
 *  Calculate the price p in the given market that minimizes the difference between the number of buy
 *  orders >= p and the number of asks <= p. Return -1 if there is no execute price found (should only
 *  occur if there were no bids/asks on that market. If multiple minima are found, pick one at random.
 *//*from  ww w.  ja  v  a 2 s  .  c o m*/
private int getExecutePrice(int marketId, OfferBook offerBook) {
    int numPrices = offerBook.getNumPrices();
    int difference = 10000;
    Vector executePriceIds = new Vector();

    for (int p = 0; p < numPrices; p++) {
        int numBids = offerBook.getGreaterThanOrEqualToBidOrders(marketId, p, -1);
        int numAsks = offerBook.getLessThanOrEqualToAskOrders(marketId, p, -1);

        int diff = Math.abs(numBids - numAsks);
        if (diff < difference && numBids != 0 && numAsks != 0) {
            difference = diff;
            executePriceIds.removeAllElements();
            executePriceIds.add(new Integer(p));
        } else if (diff == difference && numBids != 0 && numAsks != 0) {
            executePriceIds.add(new Integer(p));
        }
    }

    if (executePriceIds.size() > 0) {
        int mid = executePriceIds.size() / 2;
        log.info("Found the following execute price minima on market " + marketId + ": "
                + executePriceIds.toString() + " -- selecting one at random");

        int executePriceId = ((Integer) executePriceIds.get(mid)).intValue();
        float executePrice = offerBook.getPrice(marketId, executePriceId);

        log.info("Execute price for market " + marketId + " is: ID=" + executePriceId + ", Value="
                + executePrice);
        return executePriceId;
    } else {
        log.info("No possible transactions on market " + marketId + " -- no execute price found");
    }

    return -1;
}

From source file:EditorPaneExample10A.java

public URL[] findLinks(Document doc, String protocol) {
    Vector links = new Vector();
    Vector urlNames = new Vector();
    URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty);

    if (doc instanceof HTMLDocument) {
        HTMLDocument.Iterator iterator = ((HTMLDocument) doc).getIterator(HTML.Tag.A);
        for (; iterator.isValid(); iterator.next()) {
            AttributeSet attrs = iterator.getAttributes();
            Object linkAttr = attrs.getAttribute(HTML.Attribute.HREF);
            if (linkAttr instanceof String) {
                try {
                    URL linkURL = new URL(baseURL, (String) linkAttr);
                    if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) {
                        String linkURLName = linkURL.toString();
                        if (urlNames.contains(linkURLName) == false) {
                            urlNames.addElement(linkURLName);
                            links.addElement(linkURL);
                        }/*from www  .j a va  2  s  . co  m*/
                    }
                } catch (MalformedURLException e) {
                    // Ignore invalid links
                }
            }
        }
    }

    URL[] urls = new URL[links.size()];
    links.copyInto(urls);
    links.removeAllElements();
    urlNames.removeAllElements();

    return urls;
}

From source file:com.hihframework.core.utils.DateUtils.java

/**
 * ?// www.  j  ava2s. com
 * start
 * end?
 * 
 * 2002-05-232002-07-152002-05-23?2002-06-01?2002-07-15
 * ???
 */
public static Date[][] getMonthDividedDateList(java.sql.Date startDate, java.sql.Date endDate) {
    Date[][] result = new Date[0][2];

    if ((startDate != null) && (endDate != null)) {
        Date start = (startDate.getTime() > endDate.getTime()) ? endDate : startDate;
        Date end = (startDate.getTime() < endDate.getTime()) ? endDate : startDate;
        java.util.Vector<Object> v = new java.util.Vector<Object>();

        // 
        cal.setTime(new java.util.Date(start.getTime()));

        int month = cal.get(Calendar.MONTH);

        // ?
        cal.setTime(new java.util.Date(end.getTime()));

        long endtime = end.getTime();
        long tmptime = 0;
        v.add(start);

        do {
            // ??
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            tmptime = cal.getTime().getTime();

            if (tmptime <= endtime) {
                v.add(new Date(tmptime));

                // ?
                cal.set(Calendar.MONTH, month + 1);
                cal.set(Calendar.DAY_OF_MONTH, 1);
                tmptime = cal.getTime().getTime();

                if (tmptime <= endtime) {
                    v.add(new Date(tmptime));
                }
            }

            month++;
        } while (tmptime < endtime);

        v.add(end);
        result = new Date[v.size() / 2][2];

        for (int i = 0; i < result.length; i++) {
            result[i][0] = (Date) v.get((i * 2) + 0);
            result[i][1] = (Date) v.get((i * 2) + 1);
        }

        v.removeAllElements();
    }

    return result;
}

From source file:EditorPaneExample16.java

public URL[] findLinks(Document doc, String protocol) {
    Vector links = new Vector();
    Vector urlNames = new Vector();
    URL baseURL = (URL) doc.getProperty(Document.StreamDescriptionProperty);

    if (doc instanceof HTMLDocument) {
        Element elem = doc.getDefaultRootElement();
        ElementIterator iterator = new ElementIterator(elem);

        while ((elem = iterator.next()) != null) {
            AttributeSet attrs = elem.getAttributes();
            Object link = attrs.getAttribute(HTML.Tag.A);
            if (link instanceof AttributeSet) {
                Object linkAttr = ((AttributeSet) link).getAttribute(HTML.Attribute.HREF);
                if (linkAttr instanceof String) {
                    try {
                        URL linkURL = new URL(baseURL, (String) linkAttr);
                        if (protocol == null || protocol.equalsIgnoreCase(linkURL.getProtocol())) {
                            String linkURLName = linkURL.toString();
                            if (urlNames.contains(linkURLName) == false) {
                                urlNames.addElement(linkURLName);
                                links.addElement(linkURL);
                            }//from  w  ww  .j  a v  a2  s  . c o m
                        }
                    } catch (MalformedURLException e) {
                        // Ignore invalid links
                    }
                }
            }
        }
    }

    URL[] urls = new URL[links.size()];
    links.copyInto(urls);
    links.removeAllElements();
    urlNames.removeAllElements();

    return urls;
}

From source file:com.sshtools.common.ui.SshToolsApplicationPanel.java

/**
 * Rebuild all the action components such as toobar, context menu
 *//*  ww  w.ja v  a2  s  .c  o m*/

public void rebuildActionComponents() {
    //  Clear the current state of the component
    log.debug("Rebuild action components");
    toolBar.removeAll();
    //
    Vector enabledActions = new Vector();
    for (Iterator i = actions.iterator(); i.hasNext();) {
        StandardAction a = (StandardAction) i.next();
        String n = (String) a.getValue(Action.NAME);
        Boolean s = (Boolean) actionsVisible.get(n);
        if (s == null) {
            s = Boolean.TRUE;
        }
        if (Boolean.TRUE.equals(s)) {
            log.debug("Action " + n + " is enabled.");
            enabledActions.add(a);
        } else {
            log.debug("Action " + n + " not enabled.");
        }
    }
    //  Build the tool bar, grouping the actions
    Vector v = new Vector();
    for (Iterator i = enabledActions.iterator(); i.hasNext();) {
        StandardAction a = (StandardAction) i.next();
        if (Boolean.TRUE.equals((Boolean) a.getValue(StandardAction.ON_TOOLBAR))) {
            v.addElement(a);
        }
    }

    Collections.sort(v, new ToolBarActionComparator());
    Integer grp = null;
    for (Iterator i = v.iterator(); i.hasNext();) {
        StandardAction z = (StandardAction) i.next();
        if ((grp != null) && !grp.equals((Integer) z.getValue(StandardAction.TOOLBAR_GROUP))) {
            toolBar.add(new ToolBarSeparator());
        }
        if (Boolean.TRUE.equals((Boolean) z.getValue(StandardAction.IS_TOGGLE_BUTTON))) {
            ToolToggleButton tBtn = new ToolToggleButton(z);
            toolBar.add(tBtn);
        } else {
            ToolButton btn = new ToolButton(z);
            toolBar.add(btn);
        }
        grp = (Integer) z.getValue(StandardAction.TOOLBAR_GROUP);
    }
    toolBar.revalidate();
    toolBar.repaint();
    //  Build the context menu, grouping the actions
    Vector c = new Vector();
    contextMenu.removeAll();
    for (Iterator i = enabledActions.iterator(); i.hasNext();) {
        StandardAction a = (StandardAction) i.next();
        if (Boolean.TRUE.equals((Boolean) a.getValue(StandardAction.ON_CONTEXT_MENU))) {
            c.addElement(a);
        }
    }
    Collections.sort(c, new ContextActionComparator());
    grp = null;
    for (Iterator i = c.iterator(); i.hasNext();) {
        StandardAction z = (StandardAction) i.next();
        if ((grp != null) && !grp.equals((Integer) z.getValue(StandardAction.CONTEXT_MENU_GROUP))) {
            contextMenu.addSeparator();
        }
        contextMenu.add(z);
        grp = (Integer) z.getValue(StandardAction.CONTEXT_MENU_GROUP);
    }
    contextMenu.revalidate();
    //  Build the menu bar
    menuBar.removeAll();
    v.removeAllElements();
    for (Enumeration e = enabledActions.elements(); e.hasMoreElements();) {
        StandardAction a = (StandardAction) e.nextElement();

        if (Boolean.TRUE.equals((Boolean) a.getValue(StandardAction.ON_MENUBAR))) {
            v.addElement(a);
        }
    }
    Vector menus = (Vector) actionMenus.clone();
    Collections.sort(menus);
    HashMap map = new HashMap();
    for (Iterator i = v.iterator(); i.hasNext();) {
        StandardAction z = (StandardAction) i.next();
        String menuName = (String) z.getValue(StandardAction.MENU_NAME);
        if (menuName == null) {
            log.error("Action " + z.getName() + " doesnt specify a value for " + StandardAction.MENU_NAME);
        } else {
            String m = (String) z.getValue(StandardAction.MENU_NAME);
            ActionMenu menu = getActionMenu(menus.iterator(), m);
            if (menu == null) {
                log.error("Action menu " + z.getName() + " does not exist");
            } else {
                Vector x = (Vector) map.get(menu.name);
                if (x == null) {
                    x = new Vector();
                    map.put(menu.name, x);
                }
                x.addElement(z);
            }
        }
    }

    for (Iterator i = menus.iterator(); i.hasNext();) {
        ActionMenu m = (ActionMenu) i.next();
        Vector x = (Vector) map.get(m.name);
        if (x != null) {
            Collections.sort(x, new MenuItemActionComparator());
            JMenu menu = new JMenu(m.displayName);
            menu.setMnemonic(m.mnemonic);
            grp = null;
            for (Iterator j = x.iterator(); j.hasNext();) {
                StandardAction a = (StandardAction) j.next();
                Integer g = (Integer) a.getValue(StandardAction.MENU_ITEM_GROUP);
                if ((grp != null) && !g.equals(grp)) {
                    menu.addSeparator();
                }
                grp = g;
                if (a instanceof MenuAction) {
                    JMenu mnu = (JMenu) a.getValue(MenuAction.MENU);
                    menu.add(mnu);
                } else {
                    JMenuItem item = new JMenuItem(a);
                    menu.add(item);
                }
            }
            menuBar.add(menu);
        } else {
            log.error("Can't find menu " + m.name);
        }
    }
    menuBar.validate();
    menuBar.repaint();
}

From source file:skoa.helpers.Graficos.java

private void compararFicheros(String freferencia, String fcomparar) {
    System.out.println("min=" + rangoMinutos + ".");
    File fr = new File(ruta + freferencia); //El primer fichero es el de referencia, y del que se sacan los incrementos.
    File fc = new File(ruta + fcomparar);
    FileReader fr1 = null, fr2 = null;
    BufferedReader linea1 = null, linea2 = null;
    String L1 = null, L2 = null;//from  w w  w.  j a v a  2 s .  com
    SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    //Vamos a abrir los dos ficheros a la vez, leyendo lnea por lnea y comparando.
    try {
        File temp = new File(ruta + "temp.txt");
        if (!temp.exists())
            temp.createNewFile(); //Crea el fichero
        //LEEMOS EL PRIMER FICHERO
        fr1 = new FileReader(fr);
        linea1 = new BufferedReader(fr1);
        Vector<String> fechas = new Vector<String>(); //vector donde irn todas las fechas
        Date d1 = null, dini = null, dfin = null;
        boolean primeravez = true, nuevo = true;
        float media1 = 0;
        int nelem1 = 0, segd1 = 0;
        String fecha = "", aux = "", lant = "";
        while ((L1 = linea1.readLine()) != null) {
            aux = L1.substring(0, L1.indexOf("\t")); //Fecha de la lnea.
            d1 = formatoDelTexto.parse(aux);
            String Ssegd1 = aux.substring(aux.indexOf(" ") + 7, aux.indexOf(" ") + 9);
            segd1 = Integer.parseInt(Ssegd1);
            d1.setSeconds(segd1);
            if (nuevo) { //Si terminamos el intervalo, pasamos al siguiente.
                Calendar c = Calendar.getInstance();
                if (!primeravez) {//Si no es la primera iteracion, se guarda antes de inicializar.
                    //System.out.println(media1+"  "+nelem1);
                    media1 = media1 / nelem1;
                    BufferedWriter bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
                    String x1 = "" + media1;
                    if (!x1.contentEquals("NaN"))
                        bw.write("" + fecha + "\t" + media1 + " " + unidad + "\n");
                    bw.close();
                    media1 = nelem1 = 0;
                    String v = lant.substring(lant.indexOf("\t") + 1);
                    v = v.substring(0, v.indexOf(" "));
                    media1 = Float.parseFloat(v);//Se inicializan con los valores de la anterior linea.
                    nelem1 = 1;
                } else {
                    String u = L1.substring(L1.indexOf("\t") + 1);
                    unidad = u.substring(u.indexOf(" ") + 1);
                    lant = L1;
                    //Si es la 1 vez, tambin se inicializa la fecha inicial.
                }
                primeravez = false;
                fecha = lant.substring(0, lant.indexOf("\t"));
                fechas.add(fecha);
                //Inserta la fecha en el vector de fechas, para luego usarlo en el 2 fichero.
                Date diniaux = formatoDelTexto.parse(fecha);
                String Ssegd2 = fecha.substring(fecha.indexOf(" ") + 7, fecha.indexOf(" ") + 9);
                int segd2 = Integer.parseInt(Ssegd2);
                c.setTime(diniaux);
                dini = c.getTime();
                dini.setSeconds(segd2);
                //System.out.println("Ini="+dini);
                c.add(Calendar.MINUTE, Integer.parseInt(rangoMinutos));
                dfin = c.getTime();
                dfin.setSeconds(segd2);
                //System.out.println("Fin="+dfin);
                nuevo = false;//Esta variable se usa para definir otra vez un nuevo intervalo
            }
            if (d1.compareTo(dini) == 0) { //Fechas Iguales
                aux = L1.substring(L1.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else if (d1.compareTo(dini) > 0 && d1.compareTo(dfin) < 0) { //Est dentro del intervalo
                aux = L1.substring(L1.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else {//Si la fecha es menor que la fecha inicial o mayor que la final, se cambia de intervalo.
                nuevo = true;
            }
            lant = L1;
        }
        //guardo lo ultimo y penultimo si lo hay
        media1 = media1 / nelem1;
        BufferedWriter bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
        String x1 = "" + media1;
        String auxi = dini.toGMTString(); //d mon yyyy hh:mm:ss
        String d = auxi.substring(0, auxi.indexOf(" "));
        if (Integer.parseInt(d) < 10)
            d = "0" + d;
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String m = auxi.substring(0, auxi.indexOf(" "));
        if (m.contentEquals("Jan"))
            m = "01";
        if (m.contentEquals("Feb"))
            m = "02";
        if (m.contentEquals("Mar"))
            m = "03";
        if (m.contentEquals("Apr"))
            m = "04";
        if (m.contentEquals("May"))
            m = "05";
        if (m.contentEquals("Jun"))
            m = "06";
        if (m.contentEquals("Jul"))
            m = "07";
        if (m.contentEquals("Aug"))
            m = "08";
        if (m.contentEquals("Sep"))
            m = "09";
        if (m.contentEquals("Oct"))
            m = "10";
        if (m.contentEquals("Nov"))
            m = "11";
        if (m.contentEquals("Dec"))
            m = "12";
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String y = auxi.substring(0, auxi.indexOf(" "));
        auxi = auxi.substring(auxi.indexOf(" ") + 1);
        String h = auxi.substring(0, auxi.indexOf(" "));
        //System.out.println(y+"-"+m+"-"+d+" "+h);
        if (!x1.contentEquals("NaN"))
            bw.write("" + y + "-" + m + "-" + d + " " + h + "\t" + media1 + " " + unidad + "\n");
        bw.close();
        fechas.add(y + "-" + m + "-" + d + " " + h);
        fecha = lant.substring(0, lant.indexOf("\t"));
        if (!fecha.isEmpty()) {
            String auxr = lant.substring(lant.indexOf("\t") + 1);
            auxr = auxr.substring(0, auxr.indexOf(" "));
            media1 = Float.parseFloat(auxr);
            bw = new BufferedWriter(new FileWriter(ruta + "temp.txt", true)); //Para escribir.
            x1 = "" + media1;
            if (!x1.contentEquals("NaN"))
                bw.write("" + fecha + "\t" + media1 + " " + unidad + "\n");
            bw.close();
            fechas.add(fecha);
        }
        fr1.close();
        //for (int i=0;i<fechas.size();i++) System.out.println("*"+fechas.elementAt(i));

        //Leido el primer fichero, leo el segundo.
        File temp2 = new File(ruta + "temp2.txt");
        if (!temp2.exists())
            temp2.createNewFile(); //Crea el fichero
        fr2 = new FileReader(fc);
        linea2 = new BufferedReader(fr2);
        int pos = 0;
        String fechaf = "";
        media1 = nelem1 = 0;
        nuevo = true;
        primeravez = true;
        while ((L2 = linea2.readLine()) != null) {
            aux = L2.substring(0, L2.indexOf("\t")); //Fecha de la lnea.
            d1 = formatoDelTexto.parse(aux);
            String Ssegd1 = aux.substring(aux.indexOf(" ") + 7, aux.indexOf(" ") + 9);
            segd1 = Integer.parseInt(Ssegd1);
            d1.setSeconds(segd1);
            if (nuevo) { //Si terminamos el intervalo, pasamos al siguiente.            
                Calendar c = Calendar.getInstance();
                if (!primeravez) {//Si no es la primera iteracion, se guarda antes de inicializar.
                    media1 = media1 / nelem1;
                    BufferedWriter bw1 = new BufferedWriter(new FileWriter(ruta + "temp2.txt", true)); //Para escribir.
                    x1 = "" + media1;
                    if (!x1.contentEquals("NaN"))
                        bw1.write("" + /*fechas.elementAt(pos)+"\t"+*/media1 + " " + unidad + "\n");
                    bw1.close();
                    pos++;
                    //media1=nelem1=0;
                    String v = lant.substring(lant.indexOf("\t") + 1);
                    v = v.substring(0, v.indexOf(" "));
                    media1 = Float.parseFloat(v);//Se inicializan con los valores de la anterior linea.
                    nelem1 = 1;
                } else {
                    String u = L2.substring(L2.indexOf("\t") + 1);
                    unidad = u.substring(u.indexOf(" ") + 1);
                    lant = L1;
                    pos = 0;
                    //Si es la 1 vez, tambin se inicializa la fecha inicial.
                }
                //System.out.println(fechas.elementAt(pos));
                primeravez = false;
                fecha = fechas.elementAt(pos);
                Date diniaux = formatoDelTexto.parse(fecha);
                String Ssegd2 = fecha.substring(fecha.indexOf(" ") + 7, fecha.indexOf(" ") + 9);
                int segd2 = Integer.parseInt(Ssegd2);
                c.setTime(diniaux);
                dini = c.getTime(); //FECHA INICIAL.                    
                dini.setSeconds(segd2);

                if (pos + 1 >= fechas.size())
                    break;
                fechaf = fechas.elementAt(pos + 1);
                Date dfinaux = formatoDelTexto.parse(fechaf);
                Ssegd2 = fecha.substring(fechaf.indexOf(" ") + 7, fechaf.indexOf(" ") + 9);
                segd2 = Integer.parseInt(Ssegd2);
                c.setTime(dfinaux);
                dfin = c.getTime(); //FECHA FINAL
                dfin.setSeconds(segd2);
                //System.out.println("INI="+dini);
                //System.out.println("FIN="+dfin);
                nuevo = false;//Esta variable se usa para definir otra vez un nuevo intervalo
            }
            if (d1.compareTo(dini) == 0) { //Fechas Iguales
                aux = L2.substring(L2.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else if (d1.compareTo(dini) > 0 && d1.compareTo(dfin) < 0) { //Est dentro del intervalo
                aux = L2.substring(L2.indexOf("\t") + 1);
                aux = aux.substring(0, aux.indexOf(" "));
                media1 = media1 + Float.parseFloat(aux);
                nelem1++;
            } else {//Si la fecha es menor que la fecha inicial o mayor que la final, se cambia de intervalo.
                nuevo = true;
            }
            lant = L2;
        }
        //guardo lo ultimo si lo hay
        fecha = lant.substring(0, lant.indexOf("\t"));
        if (!fecha.isEmpty()) {
            String auxr = lant.substring(lant.indexOf("\t") + 1);
            auxr = auxr.substring(0, auxr.indexOf(" "));
            media1 = Float.parseFloat(auxr);
            BufferedWriter bw2 = new BufferedWriter(new FileWriter(ruta + "temp2.txt", true)); //Para escribir.
            x1 = "" + media1;
            if (!x1.contentEquals("NaN"))
                bw2.write("" + /*fechas.elementAt(pos+1)+"\t"+*/media1 + " " + unidad + "\n");
            bw2.close();
        }
        fr2.close();
        //CREAMOS EL UNIFICADO
        File unificado = new File(ruta + "unificado.txt");
        if (!unificado.exists())
            unificado.createNewFile(); //Crea el fichero
        fr1 = new FileReader(temp);
        linea1 = new BufferedReader(fr1);
        fr2 = new FileReader(temp2);
        linea2 = new BufferedReader(fr2);
        L1 = L2 = "";
        BufferedWriter bwf = new BufferedWriter(new FileWriter(ruta + "unificado.txt", true)); //Para escribir.
        while ((L1 = linea1.readLine()) != null && (L2 = linea2.readLine()) != null) {
            bwf.write(L1 + "\t" + L2 + "\n");
        }
        bwf.close();
        fechas.removeAllElements();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != fr1)
                fr1.close(); //cierra el 1 fichero.
            if (null != fr2)
                fr2.close(); //cierra el 2 fichero.
        } catch (Exception e2) { //Sino salta una excepcion.
            e2.printStackTrace();
        }
    }
    File temp1 = new File(ruta + "temp.txt");
    File temp2 = new File(ruta + "temp2.txt");
    temp1.delete();
    temp2.delete();
}