⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 passwordentry.java

📁 基于Eclipse RCP开发的管理工具
💻 JAVA
字号:
/*  
 * Copyright 2006 Marcel Schoffelmeer 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.s10r.manager.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.s10r.util.Observable;

/*
 * TODO: get terminology right (e.g. name == principal?)
 */
/**
 * @author schoffem
 */
public class PasswordEntry extends ManagerItem 
{

	private String id;

	private String password;

	private int usageCnt;

	private String url;

	private Date lastUsed;

	private Date lastUpdatedOnSite;

	private List<ItemLabel> labels = new ArrayList<ItemLabel>();

	public static Observable createInstance()
	{
		return new PasswordEntry("new entry", "");
	}

	public PasswordEntry(String name, String password)
	{
		super(name);

		this.password = password;

		// create values for easier comparison
		this.lastUpdatedOnSite = new Date();
	}

	/**
	 * @return the password
	 * @uml.property name="password"
	 */
	public String getPassword()
	{
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 * @uml.property name="password"
	 */
	public void setPassword(String password)
	{
		if (!password.equals(this.password))
		{
			this.password = password;
			observers.notifyEdit(this);
		}
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer();

		sb.append("PasswordEntry(");
		sb.append("name = ");
		sb.append(getName());
		sb.append(")");

		return sb.toString();
	}

	public void accept(DataVisitor visitor)
	{
		visitor.visit(this);
	}

	/**
	 * @return the id
	 * @uml.property name="id"
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 * @uml.property name="id"
	 */
	public void setId(String id)
	{
		if (!id.equals(this.id))
		{
			this.id = id;
			observers.notifyEdit(this);
		}
	}


	public int getUsageCnt()
	{
		return usageCnt;
	}

	public void setUsageCnt(int usageCnt)
	{
		if (usageCnt != this.usageCnt)
		{
			this.usageCnt = usageCnt;
			observers.notifyEdit(this);
		}
	}

	public void incUsageCount()
	{
		// note: setters will fire edit event

		setUsageCnt(getUsageCnt() + 1);

		// since we used the entry, update the last used date
		setLastUsed(new Date());
	}

	public Date getLastUsed()
	{
		return this.lastUsed;
	}

	public void setLastUsed(Date lastUsed)
	{
		if (!lastUsed.equals(this.lastUsed))
		{
			this.lastUsed = lastUsed;
			observers.notifyEdit(this);
		}
	}

	public Date getLastUpdatedOnSite()
	{
		return lastUpdatedOnSite;
	}

	public long getPasswordAge()
	{
		return (toDays(new Date().getTime() - getLastUpdatedOnSite().getTime()));
	}

	private long toDays(long msecs)
	{
		// convert long to float to make sure we have accurate rounding
		// of the number (e.g. 1.5 becomes 2 days)
		return Math.round((float) msecs / (24 * 3600 * 1000));
	}

	public void setLastUpdatedOnSite(Date lastUpdatedOnSite)
	{
		if (!lastUpdatedOnSite.equals(this.lastUpdatedOnSite))
		{
			this.lastUpdatedOnSite = lastUpdatedOnSite;
			observers.notifyEdit(this);
		}
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
		observers.notifyEdit(this);
	}

	public void userUpdatedOnSite()
	{
		this.setLastUpdatedOnSite(new Date());
	}

	public void addLabel(ItemLabel label)
	{
		if (!labels.contains(label))
		{
			labels.add(label);
			observers.notifyEdit(this);
		}
	}

	public void removeLabel(ItemLabel label)
	{
		if (labels.contains(label))
		{
			labels.remove(label);
			observers.notifyEdit(this);
		}
	}

	public List<ItemLabel> getLabels()
	{
		return labels;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj instanceof PasswordEntry)
		{
			PasswordEntry pe = (PasswordEntry) obj;

			boolean isEqual =  super.equals(obj)
					&& stringEquals(getId(), pe.getId())
					&& stringEquals(getPassword(), pe.getPassword())
					&& stringEquals(getDescription(), pe.getDescription())
					&& dateEquals(getCreated(), pe.getCreated())
					&& dateEquals(getLastUpdated(), pe.getLastUpdated())
					&& dateEquals(getLastUpdatedOnSite(), pe
							.getLastUpdatedOnSite())
					&& getUsageCnt() == pe.getUsageCnt() &&
					stringEquals(getUrl(), pe.getUrl());
						
			return isEqual;
		}
		else
		{
			return super.equals(obj);
		}
	}

	private boolean stringEquals(String s1, String s2)
	{
		if (s1 == null && s2 == null)
		{
			return true;
		}
		else if (s1 == null || s2 == null)
		{
			return false;
		}
		else
		{
			return s1.equals(s2);
		}
	}

	private boolean dateEquals(Date s1, Date s2)
	{
		if (s1 == null && s2 == null)
		{
			return true;
		}
		else if (s1 == null || s2 == null)
		{
			return false;
		}
		else
		{
			return s1.equals(s2);
		}
	}

	public String getLabelsString()
	{
		StringBuffer sb = new StringBuffer();
		for (ItemLabel label: getLabels())
		{
			if (sb.length() > 0)
			{
				sb.append(", ");
			}
			sb.append(label.getName());
		}
 		return sb.toString();
	}

	public boolean hasLabel(ItemLabel filterLabel)
	{
        return labels.contains(filterLabel);
	}

	public void removeAllLabels()
	{
		if (labels.size() > 0)
		{
		   labels.clear();	
		   
		   observers.notifyEdit(this);
		}
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -