001    package com.github.sarxos.webcam.ds.ipcam;
002    
003    import java.net.URL;
004    import java.util.ArrayList;
005    import java.util.Collections;
006    import java.util.List;
007    
008    import com.github.sarxos.webcam.WebcamDevice;
009    import com.github.sarxos.webcam.WebcamException;
010    
011    
012    /**
013     * Class used to register IP camera devices.
014     * 
015     * @author Bartosz Firyn (SarXos)
016     */
017    public class IpCamDeviceRegistry {
018    
019            /**
020             * Contains IP cameras.
021             */
022            private static final List<IpCamDevice> DEVICES = new ArrayList<IpCamDevice>();
023    
024            /**
025             * Register IP camera.
026             * 
027             * @param ipcam the IP camera to be register
028             */
029            public static void register(IpCamDevice ipcam) {
030                    for (WebcamDevice d : DEVICES) {
031                            String name = ipcam.getName();
032                            if (d.getName().equals(name)) {
033                                    throw new WebcamException(String.format("Name '%s' is already in use", name));
034                            }
035                    }
036                    DEVICES.add(ipcam);
037            }
038    
039            public static void register(String name, URL url, IpCamMode mode) {
040                    register(new IpCamDevice(name, url, mode));
041            }
042    
043            public static void register(String name, URL url, IpCamMode mode, IpCamAuth auth) {
044                    register(new IpCamDevice(name, url, mode, auth));
045            }
046    
047            public static boolean isRegistered(IpCamDevice ipcam) {
048                    for (IpCamDevice d : DEVICES) {
049                            if (d.getName().equals(ipcam.getName())) {
050                                    return true;
051                            }
052                    }
053                    return false;
054            }
055    
056            public static boolean isRegistered(String name) {
057                    for (IpCamDevice d : DEVICES) {
058                            if (d.getName().equals(name)) {
059                                    return true;
060                            }
061                    }
062                    return false;
063            }
064    
065            public static boolean isRegistered(URL url) {
066                    for (IpCamDevice d : DEVICES) {
067                            if (d.getURL().equals(url)) {
068                                    return true;
069                            }
070                    }
071                    return false;
072            }
073    
074            /**
075             * Unregister IP camera.
076             * 
077             * @param ipcam the IP camera to be unregister
078             */
079            public static void unregister(IpCamDevice ipcam) {
080                    DEVICES.remove(ipcam);
081            }
082    
083            /**
084             * Get all registered IP cameras.
085             * 
086             * @return Collection of registered IP cameras
087             */
088            public static List<IpCamDevice> getIpCameras() {
089                    return Collections.unmodifiableList(DEVICES);
090            }
091    }