# -*- coding: latin-1 -*-
""" 
   Copyright (C) 2003 PimenTech SARL (http://www.pimentech.net)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  
"""

__version__='$Revision: 1.25 $'[11:-2]

from Products.PimenTechLibCommon.map import *

from Globals import HTMLFile

class CommonSet(Object):
	" common set object "

	meta_type = "CommonSet"
	
	index_html = HTMLFile('dtml/setIndex', globals()) # View Interface

	def __repr__(self):
		xmlstr = "<%s id='%s' title='%s'>\n" % (self.meta_type, self.getId(), self.title)
		for object in self.values():
			xmlstr = "%s<element>%s</element>\n" % (xmlstr, `object`)
		return "%s</%s>" % (xmlstr, self.meta_type)

	def insert_set(self, set):
		return self.insert_map(set)

	def __iadd__(self, other):
		" UNION "
		return self.insert_map(other)
			
	def __isub__(self, other):
		for k in other.keys():
			del self[k]
		return self

	def inter(self, other):
		""" INTERSECTION """
		for k in other.keys():
			if not self.has_key(k):
				del self[k]
		return self
			
	def insert(self, object):
		"be careful, inserts only if needed !!!!!"
		
		self.message("%s (Set)%s.insert(%s)" % (self.meta_type,self.getId(),object))
		
		if (not self.has_key(object)):
			self[object] = object
			return object
		
		return self[object]


class Set(CommonSet, Map):
	"the set class"

	meta_type = 'Set'

	def manage_afterAdd(self, item, container):
		"avoid infinite recursion when added"
		self.message("%s (Set)%s.manage_afterAdd(%s,%s)" % (self.meta_type, self.getId(), item, container))
		pass

class PDictSet(CommonSet, PDictMap):
	" the set class based on persistent dict "

	meta_type = 'PDictSet'

	__init__ = PDictMap.__init__


